標準 I/O を使用してファイル内の行数をカウントする

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{     char str[50];     if(argc < 2)     {         fprintf(stderr,"使用法:%s<ファイル名>\n",argv[0]);         -1 を返します。     }     FILE *fp = fopen(argv[1],"r");     if(NULL == fp)     {         perror("fopen");         リターン-1;     }     printf("fopen 成功!\n");     int 行 = 0;     while(fgets(str,sizeof(str),fp) != NULL)     {         if(str[strlen(str)-1] == '\n')             行++;     }     printf("%s 行:%d\n",argv[1],行);



















    fclose(fp);
    0 を返します。
}

おすすめ

転載: blog.csdn.net/qq_72714790/article/details/126401694