C语言文件操作详情(二)

这篇文章主要介绍了C语言文件操作详情,文章基于上一篇文章的内容继续展开C语言文件操作的相关详情,需要的小伙伴可以参考一下

前言

接上一篇 C语言文件操作详情(一)

一、文件的顺序读写函数

fgetc和fputc

  • fgetc
    字符输入函数 适用于所有输入流
  • fputc
    字符输出函数 适用于所有输出流
int fgetc( FILE *stream ); int fputc( int c, FILE *stream );
  • fputc 写文件
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; } 
  • fgetc 读文件
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; } 

fgets和fputs

  • fges 文本行输入函数 适用于所有输入流
  • fputs 文本行输入函数 适用于所有输出流

两个函数的功能:

//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

fscanf 和 fprintf

  • fscanf 格式化输入函数 适用于所有输入流
  • fprintf 格式化输出函数 适用于所有输出流

简单对比 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/fscanf/sscanf
  • ptintf/fprintf/sprintf

scanf/printf:是针对标准输入流/标准输出流的 格式化输入/输出语句
fscnaf/fprintf:是针对所有输入流/所有输出流的 格式化输入/输出语句
sscanf/sprintfsscanf是从字符串中读取格式化的数据。sprintf是把格式化数据输出成(存储到)字符串

fraed 和 fwriite

  • fread 二进制输入函数 适用于 文件
  • fwrite 二进制输出函数 适用于 文件
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 写文件 。两个函数用法类似

二、文件的随机读写函数

前面介绍的函数都是顺序读写函数 ,有时候我们需要从某一个位置开始读写函数。下面的这些函数可以实现其功能。

fseek 函数

根据文件指针的位置和偏移量来定位文件指针

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

  • SEEK_CUR
    Current position of file pointer (文件指针的当前位置)
  • SEEK_END (文件的末尾位置)
    End of file
  • SEEK_SET (文件起始位置)
    Beginning of file

三、关于文件结束的判定

我们知道:EOF --> end of file 文件结束的标志
如果一个文件里面什么都没有我们打开文件读到的就是 “-1”,文件结束的位置有一个EOF存在。

feof函数

  • 牢记:在文件读取过程中,不能用feof函数的返回值直接来判定文件的是否结束。
    而是应用于当文件读取结束的时候,判断是读取失败结束,还是遇到文件尾EOF使文件结束。
  • 文本文件是否读取结束,判断返回值是否为EOF或则NULL
    例如:fgetc判断是否为EOF fgets判断返回值是否为NULL
  • 二进制文件的读取结束判断,判断返回值是否小于实际要读的个数
    例如:fread 判断返回值是否小于实际要读的个数

总结

到此这篇关于C语言文件操作详情(二)的文章就介绍到这了,更多相关C语言文件操作内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C语言文件操作详情(二)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言