Program to Find the Number of Lines in a Text File

Write a program to find the number of lines in a text file.

C Program


#include <stdio.h>

int main()
{

    FILE *readFile;

    //empty file have one
    //line
    int n = 0;
    char filename[10000];
    char ch;

    printf("Enter file name with full path: ");
    scanf("%s"filename);

    //open file in read mode
    readFile = fopen(filename"r");
    if (readFile == NULL)
    {
        printf("Can't open file for %s\n"filename);
        return -1;
    }
    else
    {
        ch = getc(readFile);
        while (ch != EOF)
        {
            //if next line
            if (ch == '\n')
            {
                n = n + 1;
            }
            //take next character from file.
            ch = getc(readFile);
        }
    }
    //close the file
    fclose(readFile);

    printf("Total line is %d "n);

    return 0;
}
Input:
Enter file name with full path:C:\Users\studentrecord.txt

Input File:studentrecord.txt

Name = Rahul, marks = 67 Name = Rohit, marks = 34 Name = Suman, marks = 65

Output:

Total line is 3 


No comments:

Post a Comment