文件计算统计字符数量
#include <cstdio>
#include <cstdlib>
int main(int argc, char *argv[]) {
int ch;
FILE *fp;
unsigned long count = 0;
if (argc != 2) {
printf("Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
} else if (NULL == (fp = fopen(argv[1], "r"))) {
printf("Can't open %s\n", argv[0]);
exit(EXIT_FAILURE);
}
while (EOF != (ch = getc(fp))) {
putc(ch, stdout);
count++;
}
fclose(fp);
printf("File %s has %lu characters\n", argv[1], count);
return 0;
}
运行结果:
参考文献
C Primer Plus