您的位置:首页 > 其它

【C Primer Plus】第十三章 文件的输入/输出

2011-01-09 16:59 357 查看
<span id="_xhe_cursor"></span><span id="_xhe_temp" width="0" height="0" />\/*
13.11-1 修改程序清单13.1中的程序,
使之不采用命令行参数,而是请求用户输入文件名并读入用户的响应
13.1 count.c -- 使用标准I/O
*/
#include <stdio.h>
#include <stdlib.h>     // ANSI C的 exit() 原型
#define LEN 100
int main(void)
{
int ch;         // 读取时存储每个字符的位置
char fname[LEN]; // 存储这件名
FILE *fp;       // 文件指针
long count=0;
printf("Enter your File fname:\n");
gets(fname);
if((fp = fopen(fname, "r")) == NULL)
{
printf("Can't open %s for read", fname);
exit(1);
}
while((ch = getc(fp)) != EOF)
{
putc(ch, stdout);   // 相当于putchar(ch);
count++;
}
fclose(fp);
printf("\nFile %s has %ld characters.\n", fname, count);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: