#include
int main(int argc, char *argv[])
{
if (argc < 2) //当命令行参数个数小于2时提醒输入文件名
{
fprintf(stderr, "Usage: %s
return -1;
}
FILE *fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fopen");
return -1;
}
printf("fopen success!\n");
int lines = 0;
int ch;
while (1)
{
ch = fgetc(fp);
if (EOF == ch)
break;
if ('\n' == ch) //文件有多少个换行符就有多少行
lines++;
}
printf("%s lines: %d\n", argv[1], lines);
fclose(fp);
return 0;
}