第 13 章 文件输入/输出 (标准I/O)
程序员文章站
2022-09-14 11:19:04
1 /* 2 count.c -- 使用标准 I/O 3 */ 4 5 #include 6 #include //提供 exit() 函数原型 7 8 int main(int argc, char *argv[]) 9 { 10 int ch; //读取 ......
1 /*-------------------------- 2 count.c -- 使用标准 I/O 3 --------------------------*/ 4 5 #include <stdio.h> 6 #include <stdlib.h> //提供 exit() 函数原型 7 8 int main(int argc, char *argv[]) 9 { 10 int ch; //读取文件时,储存每个字符的地方 11 FILE *fp; //文件指针 12 unsigned long count = 0; 13 14 if (argc != 2) 15 { 16 printf("Usage: %s filename\n", argv[0]); 17 exit(EXIT_FAILURE); 18 } 19 20 if (NULL == (fp = fopen(argv[1], "r"))) 21 { 22 printf("Can't open %s\n", argv[1]); 23 exit(EXIT_FAILURE); 24 } 25 26 while (EOF != (ch = getc(fp))) 27 { 28 putc(ch, stdout); //同 putchar(ch) 29 ++count; 30 } 31 32 fclose(fp); 33 printf("File %s has %lu characters\n", argv[1], count); 34 35 return 0; 36 }