代码示例_标准IO_fputc / fgetc

fputc / fgetc

 


fput_fget.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main(void)
 5 {
 6     // 打开/创建 
 7     FILE *fp = fopen("./1.text","w+");
 8     if(fp==NULL){
 9         perror("fopen failed");
10         exit(1);
11     }
12     
13     // 写字符
14     int num = 66;
15     if(  fputc(num,fp)== EOF  ){
16         perror("fputc failed");
17         exit(1);
18     }
19     
20     // 关闭
21     fclose(fp);
22 
23 
24     // 打开/创建
25     fp = fopen("./1.text","r");
26     if(fp==NULL){
27         perror("fopen failed");
28         exit(1);
29     }
30     
31     // 读字符
32     int p = fgetc(fp);
33     if(p==EOF){
34         perror("fgetc failed");
35         exit(1);
36     }
37     printf("read : %c
",p);
38 
39 
40     // 关闭
41     fclose(fp);
42 
43 
44     return 0 ;
45 }

测试:


success !

Stay hungry, stay foolish 待续。。。
原文地址:https://www.cnblogs.com/panda-w/p/11075389.html