The Wayback Machine - https://web.archive.org/web/20240602090120/https://www.geeksforgeeks.org/c-program-count-number-lines-file/
Open In App

C Program to count number of lines in a file

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

C




/* C Program to count the Number of Lines in a Text File */
#include <stdio.h>
#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;
}


Output:

Enter file name: countLines.c
The file countLines.c has 41 lines

Time complexity: O(N) where N is total number of characters in given file

Auxiliary space: O(1) because it is using constant space for variables and array filename


Similar Reads

Lex program to count the number of lines, spaces and tabs
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.Let's the how to count the number of lines, spaces and tabs using Lex. Example: Input: Geeks for Geeks gfg gf
1 min read
Implement your own tail (Read last n lines of a huge file)
Given a huge file having dynamic data, write a program to read last n lines from the file at any point without reading the entire file. The problem is similar to tail command in linux which displays the last few lines of a file. It is mostly used for viewing log file updates as these updates are appended to the log files. Source : Microsoft Intervi
4 min read
C program to copy contents of one file to another file
[GFGTABS] C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr1, *fptr2; char filename[100]; int c; printf(&quot;Enter the filename to open for reading: &quot;); scanf(&quot;%s&quot;, filename); // Open one file for reading fptr1 = fopen(filename, &quot;r&quot;); if (fptr1 == NULL) { printf(&quot;Cannot open fi
1 min read
C Program to count the Number of Characters in a File
Counting the number of characters is important because almost all the text boxes that rely on user input have certain limitations on the number of characters that can be inserted. For example, the character limit on a Facebook post is 63,206 characters. Whereas, for a tweet on Twitter the character limit is 140 characters and the character limit is
2 min read
Program to print last 10 lines
Given some text lines in one string, each line is separated by ‘\n’ character. Print the last ten lines. If number of lines is less than 10, then print all lines. Source: Microsoft Interview | Set 10 Following are the steps 1) Find the last occurrence of DELIM or '\n' 2) Initialize target position as last occurrence of '\n' and count as 0 , and do
4 min read
How to write long strings in Multi-lines C/C++?
Image a situation where we want to use or print a long long string in C or C++, how to do this? In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. C/C++ Code #include&lt;stdio.h&gt; int main() { // We can put two double quotes anywhere in a string char
2 min read
How to use getline() in C++ when there are blank lines in input?
In C++, if we need to read a few sentences from a stream, the generally preferred way is to use the getline() function as it can read string streams till it encounters a newline or sees a delimiter provided by the user. Also, it uses &lt;string.h&gt; header file to be fully functional. Here is a sample program in c++ that reads four sentences and d
2 min read
Print 2D matrix in different lines and without curly braces in C/C++?
Following is a general way of printing 2D matrix such that every row is printed in separate lines. C/C++ Code for (int i = 0; i &amp;lt; m; i++) { for (int j = 0; j &amp;lt; n; j++) { cout &amp;lt;&amp;lt; arr[i][j] &amp;lt;&amp;lt; &amp;quot; &amp;quot;; } // Newline for new row cout &amp;lt;&amp;lt; endl; } How to print without using any curly br
2 min read
Program to delete a line given the line number from a file
Given a file and a line number n, the task is to delete nth line from the given text file. Suppose the initial content of myfile.txt is : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Practice GeeksforGeeks Contribute After Deletion of Line 2 the content would be : GeeksforGeeks GeeksforGeeks IDE GeeksforGeeks Contribute Approach : 1) Open the sour
2 min read
C Program to print contents of file
fopen() is used to open and fclose() is used to close a file in C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr; char filename[100], c; printf(&quot;Enter the filename to open \n&quot;); scanf(&quot;%s&quot;, filename); // Open file fptr = fopen(filename, &quot;r&quot;); if (fptr == NULL) { printf(&quot;Ca
1 min read
Article Tags :