1 #include<stdio.h>
2
3 int main()
4 {
5 FILE *fp;
6 fp = fopen("./text2.config","w+");
7
8 // int fputc(int c, FILE *stream);
9 fputc('2',fp);
10
11 fclose(fp);
12
13 return 0;
14 }
fputc() 写整型数或者字符到文件中去
1 #include<stdio.h>
2 #include <string.h>
3
4 int main()
5 {
6 FILE *fp;
7 fp = fopen("./text3.config","w+");
8 char *buf = "hello,world yeah!";
9
10 int len = strlen(buf);
11 // int fputc(int c, FILE *stream);
12 for(int i =0;i<len;i++){
13 fputc(*buf,fp);
14 *buf++;
15 }
16
17 fclose(fp);
18
19 return 0;
20 }
~
剩下两个函数用法
把刚才写到text3.config文件中的数据读出来,读到终端
1 #include<stdio.h>
2
3 int main()
4 {
5 FILE *fp;
6 fp = fopen("./text3.config","r");
7 while(!feof(fp)){
8 int c = fgetc(fp);
9 printf("%c",c);
10 }
11 putchar('\n');
12 fclose(fp);
13
14 return 0;
15 }
~
~
=========================================================================
feof()判断是否到达文件尾巴,返回0没有到达文件尾巴
fputc()把整型数或者字符,字符串写道文件中去
fgetc()一个个字符从文件中取出来
返回值都是整型数int