C语言文件操作(中)

简介: C语言文件操作(中)

fgetc

一个一个的读:

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. int main()
6. {
7.  FILE* pf = fopen("date.txt", "r");
8.  if (pf == NULL)
9.  {
10.     printf("%s\n", strerror(errno));
11.   }
12. 
13.   //读文件(一个一个的)
14.   int ch = 0;//fgetc返回的是ASCII码值,用char也可以,但是,当文件结束的时候,返回的是EOF(-1),所以用int
15.   while ((ch = fgetc(pf)) != EOF)
16.   {
17.     printf("%c ", ch);
18.   }// 在这里,pf会自动向后移一位
19.   fclose(pf);
20.   pf = NULL;
21.   return 0;
22. }

fputs

一行一行的写:

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. int main()
6. {
7.  FILE* pf = fopen("date.txt", "w");
8.  if (pf == NULL)
9.  {
10.     printf("%s\n", strerror(errno));
11.   }
12. //一行一行的写
13.   fputs("nihao\n", pf);
14.   fputs("youyoumen\n", pf);//自己加换行符号,再加上,文本输出函数
15.   fclose(pf);
16.   pf = NULL;
17.   return 0;
18. }

fgets

读一行:

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. int main()
6. {
7.  FILE* pf = fopen("date.txt", "r");
8.  if (pf == NULL)
9.  {
10.     printf("%s\n", strerror(errno));
11.   }
12. //一行一行的读
13.   char buf[1000] = { 0 };
14.   fgets(buf, 1000, pf);//读1000-1个,仅仅显示一行。如果这一行的字符不止999个,最次用fgets,就会接着获取后面的内容;返回空指针的时候,文件结束
15.   printf("%s", buf);
16.   fclose(pf);
17.   pf = NULL;
18.   return 0;
19. }

实现一个代码,将date.txt拷贝一份生成date2.txt

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. int main()
6. {
7.  //打开
8.  FILE* pr = fopen("date.txt", "r");
9.  if (pr == NULL)
10.   {
11.     printf("open for reading:%s\n", strerror(errno));
12.     return 0;
13.   }
14.   FILE* pw = fopen("date2.txt", "w");
15.   if (pw == NULL)
16.   {
17.     printf("open for writting:%s\n", strerror(errno));
18.     fclose(pr);
19.     pr = NULL;
20.     return 0;
21.   }
22.   //拷贝文件
23.   //读一个拷贝一个
24.   int ch = 0;
25.   while ((ch = fgetc(pr)) != EOF)
26.   {
27.     fputc(ch, pw);
28.   }
29. 
30. 
31. 
32. 
33.   //关闭
34.   fclose(pw);
35.   pw = NULL;
36.   fclose(pr);
37.   pr = NULL;
38.   return 0;
39. }

fprintf

(格式化输出函数)写格式化的数据:写数据到文件里

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. struct Stu
6. {
7.  char name[20];
8.  int age;
9.  double d;
10. };
11. 
12. int main()
13. {
14.   struct Stu s = { "张三", 20, 95.5 };
15.   FILE* pf = fopen("date.txt", "w");
16.   if (pf == NULL)
17.   {
18.     printf("%s\n", strerror(errno));
19.     return 0;
20.   }
21.   //写数据
22.   fprintf(pf, "%s %d %lf", s.name, s.age, s.d);
23.   fprintf(stdout, "%s %d %lf", s.name, s.age, s.d);
24.   printf("%s %d %lf", s.name, s.age, s.d);
25. 
26.   //关闭
27.   fclose(pf);
28.   pf = NULL;
29.   return 0;
30. }

fscanf

(格式化输入函数)从文件里读数据

 

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. struct Stu
6. {
7.  char name[20];
8.  int age;
9.  double d;
10. };
11. 
12. int main()
13. {
14.   struct Stu s = { 0 };
15.   FILE* pf = fopen("date.txt", "r");
16.   if (pf == NULL)
17.   {
18.     printf("%s\n", strerror(errno));
19.     return 0;
20.   }
21.   //读数据
22.   fscanf(pf, "%s %d %lf", s.name, &(s.age), &(s.d));
23.   printf("%s %d %lf", s.name, s.age, s.d);
24. 
25.   //关闭
26.   fclose(pf);
27.   pf = NULL;
28.   return 0;
29. }

fwrite

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. struct Stu
6. {
7.  char name[20];
8.  int age;
9.  double d;
10. };
11. 
12. int main()
13. {
14.   struct Stu s[2] = { {"张三", 20, 95.5},{"李四", 19, 95 } };
15.   FILE* pf = fopen("date.txt", "wb");
16.   if (pf == NULL)
17.   {
18.     printf("%s\n", strerror(errno));
19.     return 0;
20.   }
21.   //按照二进制的方式写文件,放进二进制信息
22.   fwrite(s, sizeof(struct Stu), 2, pf);//
23. 
24.   //关闭
25.   fclose(pf);
26.   pf = NULL;
27.   return 0;
28. }

fread

1. #include <stdio.h>
2. #include <string.h>
3. #include <errno.h>
4. 
5. struct Stu
6. {
7.  char name[20];
8.  int age;
9.  double d;
10. };
11. 
12. int main()
13. {
14.   struct Stu s[2] = { 0 };
15.   FILE* pf = fopen("date.txt", "rb");
16.   if (pf == NULL)
17.   {
18.     printf("%s\n", strerror(errno));
19.     return 0;
20.   }
21.   //按照二进制的方式读文件
22.   fread(s, sizeof(struct Stu), 2, pf);//
23. 
24. 
25.   printf("%s %d %lf", s[0].name, s[0].age, s[0].d);
26.   printf("%s %d %lf", s[1].name, s[1].age, s[1].d);
27. 
28.   //关闭
29.   fclose(pf);
30.   pf = NULL;
31.   return 0;
32. }

知识点:fread的返回值是读取文件内容的块数目,不是文件总大小

相关文章
|
1月前
|
Linux C语言
C语言获取文件长度
C语言获取文件长度
|
1月前
|
存储 编译器 C语言
关于文件操作---C语言
关于文件操作---C语言
|
1月前
|
存储 程序员 C语言
C语言-文件操作
C语言-文件操作
53 2
|
1月前
|
编译器 开发工具 C语言
【C语言】第一回(源(.c)文件怎么生成可执程序(.exe)文件)
【C语言】第一回(源(.c)文件怎么生成可执程序(.exe)文件)
|
2月前
|
安全 算法 程序员
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
【C/C++ 文件操作】深入理解C语言中的文件锁定机制
49 0
|
1月前
|
存储 程序员 编译器
【C语言】深度探讨文件操作(一)
【C语言】深度探讨文件操作(一)
|
5天前
|
存储 程序员 C语言
C语言:文件操作
C语言:文件操作
11 1
|
6天前
|
存储 C语言 C++
【C语言】文件与文件操作
前言:我们通过学习的技术可以完成计算与字符串处理,但程序结束之后就都消失了,这样岂不可惜。我们通过文件与数据持久化保存相关的基础知识。
9 0
|
7天前
|
存储 编译器 C语言
C语言中的文件操作指南
C语言中的文件操作指南
12 0
|
13天前
|
算法 C语言
【C 言专栏】C 语言文件操作的技巧与方法
【4月更文挑战第30天】本文介绍了C语言文件操作的关键技巧,包括文件的打开与关闭(使用`fopen`和`fclose`函数),读取(`fgetc`、`fgets`和`fread`)和写入(`fputc`、`fputs`和`fwrite`)操作。此外,还讨论了文件指针移动(`fseek`)、错误处理、文件权限和格式等问题。文中提供了一个简单的读写文件的示例,并提到了高级技巧如随机访问、文件缓冲和截断。掌握这些技能将有助于提升C语言编程中的文件处理能力。