代码示例_标准IO_fputs / fgets

fputs_fgets


 fgets_fputs.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <strings.h>
 4 
 5 int main(void)
 6 {
 7     // 打开/创建
 8     FILE *fp = fopen("./1.text","w+");
 9     if(fp==NULL){
10         perror("fopen failed");
11         exit(1);
12     }
13 
14     
15     char buf[50];
16     bzero(buf,50);
17 
18     while(1){
19     
20     //读字符串
21     if(  fgets(buf,50,stdin)==NULL  ){
22         perror("fgets failed");
23         exit(1);
24     }
25         
26 
27     //写字符串
28     if(  fputs(buf,fp) != -1 )
29         printf("write OK !
");
30 
31     if(strncmp(buf,"quit",4)==0)
32         break;
33 
34     }
35     
36     //关闭
37     fclose(fp);
38 
39 
40     return 0 ;
41 }

测试:


success !

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