C语言:字符串输出流输出文件中的数据。

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. //定义文件指针
  6. FILE *f = NULL;
  7. //打开文件
  8. f = fopen("1.txt","wt");
  9. if(f==NULL)
  10. {
  11. printf("文件读取失败! ");
  12. return -1;
  13. }
  14. char buf[1024];
  15. //写文件
  16. while(strcmp(gets(buf),"quit")!=0)
  17. {
  18.     fputs(buf,f);
  19. fputc(' ',f);
  20. }
  21. //关闭文件
  22. fclose(f);
  23. return 0;
  24. }
原文地址:https://www.cnblogs.com/XYQ-208910/p/4708868.html