C语言程序设计(第三版)恰汗.合孜尔第9章文件课件教学.pptVIP

  • 14
  • 0
  • 约2.24万字
  • 约 89页
  • 2017-01-02 发布于未知
  • 举报

C语言程序设计(第三版)恰汗.合孜尔第9章文件课件教学.ppt

9.7 程序举例 例9.14 编写一个程序实现输入一个字符串,将该字符串写入文件中,然后统计字符串中有多少个空格。 #include stdio.h #include stdlib.h void main() { FILE *fp; /* 定义一个文件指针变量fp */ char ch; int count=0; if((fp=fopen(c:\\testfile.txt,w))==NULL) { printf(cannot open file!\n); exit(0); } printf(please enter string:); while((ch=getchar())!=\n) /* 此循环用于将字符串写入文件 */ fputc(ch,fp); fclose(fp); if((fp=fopen(c:\\testfile.txt,r))==NULL) { printf(cannot open file!\n); exit(0); } while((ch=fgetc(fp))!=EOF) /* 此循环用于统计字符串中的空格数 */ { if(ch==32) count++;

文档评论(0)

1亿VIP精品文档

相关文档