1. 字符读写函数fgetc和fputc
- fgets(fp) fp为文件句柄
- fputs(ch,fp) ch为字符变量,fp为句柄,成功返回相应的字符,失败返回EOF。
fgetc处理的是窄字符。 - 文件指针使用之后一定要关闭,否则会造成文件没有写进去。
2. 对文件读写
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> void main1() { FILE *pf = fopen("C:\\Users\\sjx\\Desktop\\test\\Q.txt", "r"); //char ch = fgetc(pf); while (!feof(pf)) { char ch = fgetc(pf);//读取, putchar(ch); } fclose(pf); system("pause"); } void main2() { FILE *pfr = fopen("C:\\Users\\sjx\\Desktop\\test\\Q.txt", "r"); FILE *pfw = fopen("C:\\Users\\sjx\\Desktop\\test\\QW1.txt", "w"); while (!feof(pfr)) { char ch = fgetc(pfr);//读取, fputc(ch, pfw); } fclose(pfr); fclose(pfw); system("C:\\Users\\sjx\\Desktop\\test\\QW1.txt"); system("pause"); } void main3() { FILE *pfr = fopen("C:\\Users\\sjx\\Desktop\\test\\Q.exe", "rb");//二进制需要加b FILE *pfw = fopen("C:\\Users\\sjx\\Desktop\\test\\fq.exe", "wb"); while (!feof(pfr)) { char ch = fgetc(pfr);//读取, fputc(ch, pfw); } fclose(pfr); fclose(pfw); system("C:\\Users\\sjx\\Desktop\\test\\fq.exe"); system("pause"); }
时间: 2024-08-29 18:09:42