主函数
#include <stdio.h>
int file_line(char *file)
{
FILE *fp = fopen(file,"r");
int count = 0;
while (1)
{
char str[30] = "";
if (fgets(str,sizeof(str),fp) != NULL)
{
count++;
}
else
{
break;
}
}
fclose(fp);
return count;
}
int main(int argc,char *argv[])
{
if (argc != 2)
{
printf("输出错误!\n");
return -1;
}
printf("文件有%d行\n",file_line(argv[1]));
return 0;
}
测试
root@VM-12-9-ubuntu:7-27# a 1.c
文件有4行
root@VM-12-9-ubuntu:7-27# cat -n 2.c
1 hello
2 world
3 hello
4 china