C语言文件操作(2)
一、文件的读写
①顺序读写
函数名 | 功能 | 适用于 |
---|---|---|
fgetc | 字符输入函数 | 适用于所有输入流 |
fputc | 字符输出函数 | 适用于所有输出流 |
fgets | 文本行输入函数 | 适用于所有输入流 |
fputs | 文本行输出函数 | 适用于所有输出流 |
sscanf | 格式化输入函数 | 适用于所有输入流 |
sprintf | 格式化输出函数 | 适用于所有输出流 |
fread | 二进制输入函数 | 文件流 |
fwrite | 二进制输出函数 | 文件流 |
让我们举几个例子一起来感受下:
int fgetc ( FILE * stream );
//例1
int main()
{
int ch = 0;
ch = fgetc(stdin);//返回的是读取字符的ASCII码值
printf("%c\n", ch);//此时的作用和getchar是一样的,因为fgetc适用于所有输入流。
return 0;
}
//例2
int main()
{
FILE* fp = fopen("test.txt", "r");//abcdef
if (fp == NULL)
{
perror("fopen");
return 1;
}
int ch = 0;
ch = fgetc(fp);
printf("%c\n", ch);//a
ch = fgetc(fp);
printf("%c\n", ch);//b
ch = fgetc(fp);
printf("%c\n", ch);//c,说明了是一个一个往后读的,自动向后挪一位
ch = fgetc(fp);
printf("%c\n", ch);//d
ch = fgetc(fp);
printf("%c\n", ch);//e
ch = fgetc(fp);
printf("%c\n", ch);//f
ch = fgetc(fp);
printf("%d\n", ch);//文件结束标志EOF,返回-1
return 0;
}
//例3
int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputs("Hello World\n", pf);
fputs("加油\n", pf);
fput(65, pf);
fclose(pf);
pf = NULL;
return 0;
}
//例4
//前情提要
//int main()
//{
// FILE* pf = fopen("test.txt", "w");
// if (pf == NULL)
// perror("fopen");
// return 1;
// }
// fputs("Hello World\n", pf);
// fputs("abc\n", pf);
// fputc(65, pf);
// fclose(pf);
// pf = NULL;
// return 0;
//}
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
char arr[20] = { '\0' };
char arr1[20] = { '\0' };
char arr2[20] = { '\0' };
char arr3[20] = { '\0' };
fgets(arr, 5, pf);//放进去4个,加'\0',读一行
printf("%s", arr);//hell
fgets(arr1, 5, pf);
printf("%s", arr1);//o wo
fgets(arr2, 10, pf);
printf("%s", arr2);//rld和换行符,之后无论大小,就都不向下读取了
fgets(arr3, 4, pf);
printf("%s", arr3);//abc
fclose(pf);
pf = NULL;
return 0;
}
例5:
C语言实现文件复制
这个是我的另一篇博客,希望大家多多点赞呀!
//例6
struct S
{
int i;
char a;
char arr[20];
};
int main()
{
struct S stu = { 1, 'a', "abc" };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fprintf(pf, "%d %c %s", stu.i, stu.a, stu.arr);
fclose(pf);
pf = NULL;
return 0;
}
//例7
struct S
{
int i;
char a;
char arr[20];
};
int main()
{
struct S stu = { 0 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fscanf(pf, "%d %c %s", &(stu.i), &(stu.a), &(stu.arr));
printf("%d %c %s", stu.i, stu.a, stu.arr);//1, a, abc
fclose(pf);
pf = NULL;
return 0;
}
//例8
int main()
{
int arr[10] = { 1, 2, 3, 4, 5, 6, 7 };
FILE* fp = fopen("test.txt", "wb");
if (fp == NULL)
{
perror("fopen");
return 1;
}
fwrite(arr, sizeof(int), 7, fp);
return 0;
}
这就是以二进制形式不加转换直接呈现的二进制文件,有点抽象吧,那我们用二进制编译器打开看一下。
具体步骤可以参考这篇博客哦,别忘了点赞呀!!!
C语言文件操作(1)
这样的1 2 3 4 5 6 7就很明显了
②随机读写
fseek
根据文件指针的位置和偏移量来定位文件指针。
int fseek ( FILE * stream, long int offset, int origin );
stream: 指向FILE对象的指针,表示要操作的文件流。
offset:表示要移动的字节数,可以是正数、负数或零。也就是偏移量。
origin:表示起始位置,可以取以下值:
SEEK_SET:从文件开头开始计算偏移量。
SEEK_CUR:从当前位置开始计算偏移量。
SEEK_END:从文件末尾开始计算偏移量。
起始位置是第一个元素,偏移量是0;终止位置是’\0’
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, 9, SEEK_SET);
fputs(" sam", pFile);
fclose(pFile);
pFile = NULL;
return 0;
}
ftell
返回文件指针相对于起始位置的偏移量
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, -2, SEEK_END);
long int a = ftell(pFile);
fputs(" sam", pFile);
fclose(pFile);
pFile = NULL;
printf("%d\n", a);//15
return 0;
}
rewind
让文件指针的位置回到文件的起始位置
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, 9, SEEK_SET);
rewind(pFile);
long int a = ftell(pFile);
fputs(" sam", pFile);
fclose(pFile);
pFile = NULL;
printf("%d\n", a);//0
return 0;
}
二、文件读取结束的判断标志
重点:
被错误使用的feof
牢记:在文件读取过程中,不能用feof函数的返回值直接来判断文件的是否结束。
feof 的作用是:当文件读取结束的时候,判断是读取结束的原因是否是遇到文件尾结束。
异常返回
1.文本文件
判断文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者NULL ( fgets )
例如:
fgetc 判断是否为 EOF .
fgets 判断返回值是否为 NULL .
int main(void)
{
int c; // 注意:int,非char,要求处理EOF
FILE* fp = fopen("test.txt", "r");
if (!fp) {
perror("File opening failed");
return 1;
}
//fgetc 当读取失败的时候或者遇到文件结束的时候,都会返回EOF
while ((c = fgetc(fp)) != EOF) // 标准C I/O读取文件循环
{
putchar(c);
}
//判断是什么原因结束的
if (ferror(fp))
puts("I/O error when reading");
else if (feof(fp))
puts("End of file reached successfully");
fclose(fp);
}
2.二进制文件
判断二进制文件是否结束
fread判断返回值是否小于实际要读的个数。
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
返回值就是成功读取的个数