目录前言一、文件的顺序读写函数fgetc和fputcfgets和fputsfscanf 和 fprintf对比一组函数fraed 和 fwriite二、文件的随机读写函数fseek
接上一篇 C语言文件操作详情(一)
int fgetc( FILE *stream );
int fputc( int c, FILE *stream );
int main()
{
FILE* pfWrite = fopen("test.txt", "w");
if (pfWrite == NULL)//检验
{
printf("%s\n", strerror(errno));//如果写文件错误打印错误原因
return 0;
}
//写文件
fputc("a", pfWrite);
fputc("b", pfWrite);
fputc("c", pfWrite);
//关闭文件
fclose(pfWrite);
pfWrite == NULL;
return 0;
}
int main()
{
FILE* pfRead = fopen("test.txt", "r");
if (pfRead == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
printf("%s", fgetc(pfRead));//a
printf("%s", fgetc(pfRead));//b
printf("%s", fgetc(pfRead));//c
//关闭文件
fclose(pfRead);
pfRead == NULL;
return 0;
}
两个函数的功能:
//Get a string from a stream. 从流中获取字符串。
char *fgets(char *string, int n, FILE *stream);
//Write a string to a stream. 将字符串写入流。
int fputs(const char *string, FILE *stream);
用fgets函数读取一行:
int main()
{
char buff[100] = { 0 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
return 0;
}
//读文件
fgets(buff, 100, pf);
printf("%s", buff); //注意buff里面本身就有一个换行符 (文件里面末尾有换行符)
//打开成功,读文件,关闭文件
fclose(pf);
pf == NULL;
return 0;
}
读取一行打印到显示器上 如图:
用fputs函数写一行:
int main()
{
char buff[100] = { 0 };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//写文件
fputs("hello\n", pf);
fputs("Word\n", pf);
//打开成功,写文件,关闭文件
fclose(pf);
pf = NULL;
return 0;
}
用fputs 写一行到文件里面:如下:
fgets和fputs也可以操作键盘和屏
键盘输入abcd 回车 屏幕打印 abcd
简单对比 scanf 和 fscanf,printf 和 fprintf,用法都非常相似
int scanf( const char *fORMat [,argument]... );
int fscanf( FILE *stream, const char *format [, argument ]... );
int printf( const char *format [, argument]... );
int fprintf( FILE *stream, const char *format [, argument ]...);
fprintf 写文件
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100, 3.14, "abc" };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//格式化的形式写文件
fprintf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
fscanf 读文件
struct S
{
int n;
float score;
char arr[10];
};
int main()
{
struct S s = { 100, 3.14, "abc" };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
return 0;
}
//格式化的形式输入数据
fscanf(pf, "%d %f %s", s.n, s.score, s.arr);
fclose(pf);
pf = NULL;
return 0;
}
当然fscanf和fprintf函数同样可以运用于标准的输入输出流(stdin,stdout)
scanf/printf:是针对标准输入流/标准输出流的 格式化输入/输出语句
fscnaf/fprintf:是针对所有输入流/所有输出流的 格式化输入/输出语句
sscanf/sprintf:sscanf是从字符串中读取格式化的数据。sprintf是把格式化数据输出成(存储到)字符串
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
struct S{
char name[20];
int age;
double score;
};
int main()
{
struct S s = { "张三", 20, 55.6 };
FILE* pf = fopen("test.txt", "wb");
if (pf == NULL)
{
return 0;
}
//二进制形式写文件
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
fread 读文件 ,fwrite 写文件 。两个函数用法类似
前面介绍的函数都是顺序读写函数 ,有时候我们需要从某一个位置开始读写函数。下面的这些函数可以实现其功能。
根据文件指针的位置和偏移量来定位文件指针
Moves the file pointer to a specified location.
int fseek( FILE *stream, long offset, int origin );
//三个参数所指的内容:pf 偏移量 文件指针的当前位置
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
return 0;
}
//定位文件指针
fseek(pf,2,SEEK_CUR);//偏移量为2
//读取文件
int ch = fgetc(pf);
prinf("%c\n",ch);
fclose(pf);
pf = NULL;
return 0;
}
如图从文件指针的当前位置 a 前面开始偏移量为2,打印c
我们知道:EOF --> end of file 文件结束的标志
如果一个文件里面什么都没有我们打开文件读到的就是 “-1”,文件结束的位置有一个EOF存在。
feof函数
到此这篇关于C语言文件操作详情(二)的文章就介绍到这了,更多相关C语言文件操作内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C语言文件操作详情(二)
本文链接: https://www.lsjlt.com/news/147472.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0