📜  C程序来计算文件中的行数

📅  最后修改于: 2021-05-25 23:33:46             🧑  作者: Mango

/* C Program to count the Number of Lines in a Text File  */
#include 
#define MAX_FILE_NAME 100
  
int main()
{
    FILE *fp;
    int count = 0;  // Line counter (result)
    char filename[MAX_FILE_NAME];
    char c;  // To store a character read from file
  
    // Get file name from user. The file should be
    // either in current folder or complete path should be provided
    printf("Enter file name: ");
    scanf("%s", filename);
  
    // Open the file
    fp = fopen(filename, "r");
  
    // Check if file exists
    if (fp == NULL)
    {
        printf("Could not open file %s", filename);
        return 0;
    }
  
    // Extract characters from file and store in character c
    for (c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n') // Increment count if this character is newline
            count = count + 1;
  
    // Close the file
    fclose(fp);
    printf("The file %s has %d lines\n ", filename, count);
  
    return 0;
}

输出:

Enter file name: countLines.c
The file countLines.c has 41 lines
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。