1、fputc and fgetc: 按顺序单个读写
int main()
{
// 绝对路径
// file* pf = fopen("c:\\users\\fami7z\\source\\repos\\文件学习\\test.txt", "r");
// 相对路径
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// write into file:
fputc('f', pf);
fputc('a', pf);
fputc('m', pf);
fputc('i', pf);
// read from file:
int ret = fgetc(pf);
printf("%c ", ret);
ret = fgetc(pf);
printf("%c ", ret);
ret = fgetc(pf);
printf("%c ", ret);
ret = fgetc(pf);
printf("%c ", ret);
ret = fgetc(pf);
printf("%c ", ret);
// 从标准输入流按顺序读入数据
int retfromkeyboard = fgetc(stdin);
printf("%c ", retfromkeyboard);
retfromkeyboard = fgetc(stdin);
printf("%c ", retfromkeyboard);
retfromkeyboard = fgetc(stdin);
printf("%c ", retfromkeyboard);
retfromkeyboard = fgetc(stdin);
printf("%c ", retfromkeyboard);
retfromkeyboard = fgetc(stdin);
printf("%c ", retfromkeyboard);
// close
fclose(pf);
pf = NULL;
return 0;
}
2、fputs and fgets : 按顺序读写一行数据
int main()
{
FILE* pf = fopen("text1.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 按行写入数据
//fputs("fami\n", pf);
//fputs("wakatipu\n", pf);
// 按行读取数据
char arr1[5] = { 0 };
char arr2[5] = { 0 };
fgets(arr1, 5, pf);
printf("%s\n", arr1);
fgets(arr2, 5, pf); // 此行读取的是 "\n"
printf("%s\n", arr2);
fgets(arr2, 5, pf);
printf("%s\n", arr2);
// 关闭
fclose(pf);
pf = NULL;
return 0;
}
3、fprintf and fscanf : 按顺序按格式读写文件
int main()
{
struct s stu = { "fami",23,"2021202120" };
FILE* pf = fopen("按格式读写.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 按格式写入数据
fprintf(pf, "%s %d %s", stu.name, stu.age, stu.id);
// 关闭
fclose(pf);
pf = NULL;
return 0;
}
int main()
{
struct s stu = { 0 };
FILE* pf = fopen("按格式读写.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 按格式读数据
fscanf(pf, "%s %d %s", stu.name, &stu.age, stu.id);
fputs("file contents:\n",stdout);
//printf("%s %d %s\n", stu.name, stu.age, stu.id);
fprintf(stdout, "%s %d %s\n", stu.name, stu.age, stu.id);
// 关闭
fclose(pf);
pf = NULL;
return 0;
}
4、fread and fwrite : 按顺序按二进制读写文件
struct s
{
char name[10];
int age;
char id[20];
};
int main()
{
struct s stu[2] = { {"fami",23,"2021202120" },{"waka",24,"2017201720"}};
FILE* pf = fopen("按二进制读写.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 按二进制写入数据
fwrite(&stu, sizeof(struct s), 2, pf);
// 关闭
fclose(pf);
pf = NULL;
return 0;
}
// attention: 二进制读写时,以文本格式打开,字符的显示不是乱码
int main()
{
struct s stu[2] = { 0 };
FILE* pf = fopen("按二进制读写.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 按二进制读数据
fread(&stu, sizeof(struct s), 2, pf);
fputs("file contents:\n", stdout);
fprintf(stdout, "%s %d %s\n", stu[0].name, stu[0].age, stu[0].id);
fprintf(stdout, "%s %d %s\n", stu[1].name, stu[1].age, stu[1].id);
// 关闭
fclose(pf);
pf = NULL;
return 0;
}
5、fseek (SEEK_CUR/SEEK_END/SEEK_SET) rewind: 随机读写文件
int main()
{
FILE* pf = fopen("new.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// read 读一个数据后,SEEK_POS 自动加 1
int ch = fgetc(pf);
printf("%c\n", ch);
// offset = 2
fseek(pf, 2, SEEK_CUR);
ch = fgetc(pf);
printf("%c\n", ch);
fseek(pf, 1, SEEK_CUR);
ch = fgetc(pf);
printf("%c\n", ch);
rewind(pf); // 回到起始位置
ch = fgetc(pf);
printf("%c\n", ch);
fseek(pf, -1, SEEK_END);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
6、ferror and feof : feof 不能用于判断文件是否读到末尾处
// 将source.txt的内容拷贝到dest.txt
int main()
{
FILE* pfread = fopen("source.txt", "r");
if (!pfread)
{
perror("fopen->pfread");
return 1;
}
FILE* pfwrite = fopen("dest.txt", "w");
if (!pfwrite)
{
fclose(pfread);
pfread = NULL;
perror("fopen->pfwrite");
return 2;
}
// copy and paste
int ch = 0;
while ((ch = fgetc(pfread)) != EOF)
{
fputc(ch, pfwrite);
}
// 判断 EOF or error
if (ferror(pfread))
printf("读文件遇到错误!\n");
else if (feof(pfread))
printf("遇到文件结尾,读文件结束!");
//
fclose(pfread);
pfread = NULL;
fclose(pfwrite);
pfwrite = NULL;
return 0;
}
7、文本文件和二进制文件
int main()
{
FILE* pf = fopen("new1.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return 1;
}
// 以二进制形式写入文件
int a = 10000;
fwrite(&a, sizeof(int), 1, pf);
//
fclose(pf);
pf = NULL;
return 0;
}
vs可用二进制编辑器打开二进制格式的文件。
8、文件缓冲区
#define _CRT_SECURE_NO_WARNINGS -1
#include <stdio.h>
#include <Windows.h> // sleep
// 文件缓冲区
int main()
{
FILE* pf = fopen("test_file_buffer.txt", "w");
fputs("abcdef", pf);//先将代码放在输出缓冲区
printf("睡眠10秒-已经写数据了(在缓冲区),打开test.txt文件,发现文件没有内容\n");
Sleep(10000);
printf("刷新缓冲区\n");
fflush(pf);//刷新缓冲区时,才将输出缓冲区的数据写到文件(磁盘)
//注:fflush在高版本的vs上不能使用了
printf("再睡眠10秒-此时,再次打开test.txt文件,文件有内容了\n");
Sleep(10000);
fclose(pf);
//注:fclose在关闭文件的时候,也会刷新缓冲区
pf = NULL;
return 0;
}