(4)fputs()
(5)fprintf()
(6)fscanf()
(7)fwrite()
(8)fread()
4.1 对比一组函数
4.1.1 了解sscanf()与sprintf()函数
5.1 fseek()
5.2 ftell()
5.3 rewind()
文本文件和二进制文件
文件读取结束的判定
7.1 被错误使用的feof
8.文件缓冲区
8.1 什么是文件缓冲区
8.2 缓冲区存在的意义
8.3 缓冲区的分类
作用:写一个字符串到文件流里去
#include<stdio.h>
#include<errno.h>
int main()
{
char arr[1024] = { 0 };
FILE* pf = fopen("text.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
fputs("hello", pf);
fputs("world", pf);
fclose(pf);
pf = NULL;
}
运行后文件截图:
注意:fputs()函数不会自动带换行符
函数fgets()和函数fputs()函数直接应用举例:
#include<stdio.h>
int main()
{
//从键盘读取一行信息
char buf[1024] = { 0 };
fgets(buf, 1024, stdin);//从标准输入流读取
fputs(buf,stdout);//输出到标准输出流
//上面这两行等价于下面的这两行
//gets(buf);
//puts(buf);
return 0;
}
运行截图:
上面之所以出现一个换行符是因为我们在输入的末尾输入了一个换行符。
对比一下printf()
使用举例:
#include<stdio.h>
#include<string.h>
#include<errno.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14f,"bit" };
FILE* pf = fopen("text.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fprintf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
运行后截图:
对比scanf()
使用举例:
#include<stdio.h>
#include<string.h>
#include<errno.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = {0};
FILE* pf = fopen("text.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fscanf(pf, "%d %f %s", &s.n, &s.score, &s.arr);
printf("%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
函数fprintf()和fscanf()的直接应用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = {0};
fscanf(stdin, "%d %f %s", &s.n, &s.score, &s.arr);
fprintf(stdout, "%d %f %s", s.n, s.score, s.arr);
return 0;
}
运行截图:
** buffer:指针指向被写入的数据**
size:被写入元素的大小,单位是字节
count:被写入的数据的数目
stream:写入的目的流
使用举例:
#include<stdio.h>
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "张三",20,55.6 };
FILE* pf = fopen("text.txt", "wb");
if (pf == NULL)
{
return 0;
}
//以二进制的形式写文件
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
** buffer:存储数据的位置**
size:元素的大小
count:读取元素的数目
stream:指针指向的文件结构
使用举例:
#include<stdio.h>
struct S
{
char name[20];
int age;
double score;
};
int main()
{
struct S tmp = { 0 };
FILE* pf = fopen("text.txt", "rb");
if (pf == NULL)
{
return 0;
}
//以二进制的形式读文件
fread(&tmp, sizeof(struct S), 1, pf);
printf("%s %d %lf", tmp.name, tmp.age, tmp.score);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
scanf/fscanf/sscanf printf/fprintf/sprintf
(1)sscanf()
作用描述:从一个字符串中读取一段格式化的数据。
使用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14,"hello"};
struct S tmp = { 0 };
char buf[1024] = { 0 };
//把格式化的数据转换成字符串存储到buf中
sprintf(buf, "%d %f %s", s.n, s.score, s.arr);
//从buf中读取格式化的数据到tmp中
sscanf(buf, "%d %f %s", &(tmp.n), &(tmp.score), &(tmp.arr));
printf("%d %f %s\n", tmp.n, tmp.score, tmp.arr);
return 0;
}
运行截图:
(2)sprintf()
作用描述:将格式化的数据写入到一个字符串中去。
使用举例:
#include<stdio.h>
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100,3.14,"hello"};
char buf[1024] = { 0 };
sprintf(buf, "%d %f %s", s.n, s.score, s.arr);
printf("%s", buf);
return 0;
}
scanf/printf 是针对标准输入流/标准输出流的 格式化输入/输出语句。
fscanf/fprintf 是针对所有输入流/所有输出流的 格式化输入输出语句,包含上面函数的功能。
sscanf/sprintf sscanf是从字符串中读取格式化的数据,sprintf是把格式化的数据输出成(存储到)字符串。
作用:移动文件指针到一个特定的位置
stream:要移动的文件指针
offset:偏移量(可以为负数,整数就是向右偏移,负数就是向左偏移)
origin:文件指针的当前位置
orifon起始位置的三个选择:
SEEK_CUR:文件指针的当前位置
SEEK_END:文件的末尾位置(在最后一个字符的后面,比如在下面的例子中就是在f的后面)
SEEK_SET:文件的起始位置
使用举例:
文件初始截图:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//1.定位文件指针
fseek(pf, 2, SEEK_CUR);
//2.读取文件
int ch = fgetc(pf);
printf("%c", ch);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
作用:计算文件指针举例相对起始位置的偏移量
使用举例:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//1.定位文件指针
fseek(pf, 2, SEEK_CUR);
//2.计算偏移量
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
注意:fgetc()函数读取过后文件指针会自动向后偏移一位
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fgetc(pf);
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
作用:使文件指针回到文件的起始位置(此时举例文件起始位置的偏移量为0)。
使用举例:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fgetc(pf);
rewind(pf);
int pos = ftell(pf);
printf("%d\n", pos);
fclose(pf);
pf = NULL;
return 0;
}
运行截图:
根据数据的组织形式,数据文件被称为文本文件或者二进制文件。
数据在内存中以二进制的形式存储,如果不加转换的输出到外存,就是二进制文件。
如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。
一个数据在内存中是怎么存储的呢?
字符一律以ASCII形式存储,数值型数据既可以用ASCII形式存储,也可以使用二进制形式存储。
如有整数10000,如果以ASCII码的形式输出到磁盘,则磁盘中占用5个字节(每个字符一个字节),而 二进制形式输出,则在磁盘上只占4个字节(VS2013测试)。
10000在内存中的存储形式:10 27 00 00(16进制形式显示的)
测试代码:
#include <stdio.h>
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);
pf = NULL;
return 0;
}
牢记:在文件读取过程中,不能用feof函数的返回值直接用来判断文件的是否结束。 而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾结束。
文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets ),如果是,就返回一个非0的值。 例如: fgetc 判断是否为 EOF fgets 判断返回值是否为 NULL
二进制文件的读取结束判断,判断返回值是否小于实际要读的个数。 例如: fread判断返回值是否小于实际要读的个数。
正确的使用: 文本文件的例子:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c; // 注意:int,非char,要求处理EOF
FILE* fp = fopen("test.txt", "r");
if (!fp) {
perror("File opening failed");
return EXIT_FAILURE;
}
//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);
}
二进制文件的例子:
#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{
double a[SIZE] = { 1.,2.,3.,4.,5. };
FILE* fp = fopen("test.bin", "wb"); // 必须用二进制模式
fwrite(a, sizeof * a, SIZE, fp); // 写 double 的数组
fclose(fp);
double b[SIZE];
fp = fopen("test.bin", "rb");
size_t ret_code = fread(b, sizeof * b, SIZE, fp); // 读 double 的数组
if (ret_code == SIZE) {
puts("Array read successfully, contents: ");
for (int n = 0; n < SIZE; ++n) printf("%f ", b[n]);
putchar('\n');
}
else { // error handling
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp)) {
perror("Error reading test.bin");
}
}
fclose(fp);
}
ANSIC 标准采用“缓冲文件系统”处理的数据文件的,所谓缓冲文件系统是指系统自动地在内存中为程序 中每一个正在使用的文件开辟一块“文件缓冲区”。从内存向磁盘输出数据会先送到内存中的缓冲区,装 满缓冲区后才一起送到磁盘上。如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓 冲区(充满缓冲区),然后再从缓冲区逐个地将数据送到程序数据区(程序变量等)。缓冲区的大小根 据C编译系统决定的。
#include <stdio.h>
#include <windows.h>
//VS2013 WIN10环境测试
int main()
{
FILE* pf = fopen("test.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;
}
这里可以得出一个结论: 因为有缓冲区的存在,C语言在操作文件的时候,需要做刷新缓冲区或者在文件操作结束的时候关闭文 件。 如果不做,可能导致读写文件的问题。
首先,把若干字符作为一个块传输比逐个发送字符节省时间;其次,如果用户打错字符,可以直接通过键盘修改错误。当最后按下Enter键时,发送的才是正确的输入。
C语言在不同的地方根据需要使用不同的缓冲区。
缓冲区分为两类:完全缓冲I/O和行缓冲I/O和不带缓冲。
(1)完全缓冲是指的是当前缓冲区被填满时才刷新缓冲区(内容被发送至目的地),通常出现在文件输入中。缓冲区的大小取决于系统,常见的大小是512字节和4096字节。
(2)行缓冲指的是在出现换行时刷新缓冲区。键盘输入通常是行缓冲输入,所以在按下Enter键时才刷新缓冲区。常见的例子就是getchar()函数,当程序调用getchar()函数时,程序就等着用户按键,用户输入的字符被存放在键盘缓冲区中,直到用户按回车为止(回车字符也放在缓冲区中)。当用户键入回车之后,getchar()函数才开始从键盘缓冲区中每次读入一个字符。也就是说,后续的getchar()函数调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完后,才重新等待用户按键。当缓冲区满时不会自动刷新且无法继续输入,需要按回车才可以刷新缓冲区。
(3)不带缓冲:标准输出不带缓冲(例如:cerr
(4)缓冲区的刷新(执行真正的I/O操作)
当缓冲区满时:执行flush语句;执行end语句;关闭文件。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_57304511/article/details/122745619
内容来源于网络,如有侵权,请联系作者删除!