📜  获取文件线数 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:40.104000             🧑  作者: Mango

代码示例1
#include 

int main()
{

    FILE *fileptr;
    int count_lines = 0;
    char filechar[40], chr;


    printf("Enter file name: ");
    scanf("%s", filechar);
    fileptr = fopen(filechar, "r");
   //extract character from file and store in chr
    chr = getc(fileptr);
    while (chr != EOF)
    {
        //Count whenever new line is encountered
        if (chr == 'n')
        {
            count_lines = count_lines + 1;
        }
        //take next character from file.
        chr = getc(fileptr);

    }
    fclose(fileptr); //close file.
    printf("There are %d lines in %s  in a file\n", count_lines, filechar);
    return 0;
}