C Program to Find the Longest Line in a File


In this post, there is a c program to find the longest line in a file. This is a very important c program of file handling. You must know what is fgets() function in c programming. So, before writing this program, let us see the sample file which contains the number of lines.

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

Input file: abc.txt

This is the input file and the contents of this file are

file handling
welcome to c programming
how to read a file
what is fgets function?

As you can see the contents of the above file, there are five lines and the longest line is “welcome to c programming”. But, we need to print this line using c program.

Also Read: Program in C to Replace Capital C with Capital S in a File

C Program to Find the Longest Line in a File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main( )
  FILE *fp;
  char str[100],longest[100];
  int len=0;
      return 0;
  printf("Longest Line is %s",longest);
  return 0;


Longest Line is welcome to c programming

Detailed Explanation

The detailed explanation of this program is as follows.

FILE *fp;

This is the file pointer of type FILE. We open a file using this file pointer.

char str[100], longest[100];

These are two character array variables. The string str stores each line and the string longest will store the longest line.

int len=0;

This variable len will store length of a string. We will use the value of this len to print the longest line.

Also Read: C Program to Remove White Spaces and Comments from a File


Here, we are opening a file abc.txt into the buffer for reading and associate this file to the file pointer fp.


This statement will verify is there any error while opening a file. If the value of fp is NULL that means there is an error while opening a file.

How to find the longest line in a file?

This is the main question. Following code is actually finding the longest line in a file.

while( fgets(str, sizeof(str), fp) != NULL )

In the while loop, we have used fgets() function. This function reads the string from the file which is associated with fp. The sizeof() will return the maximum number of bytes and those number of characters are stored in the variable str.

Also Read: C Program to Copy the Contents of One File into Another File

When there is no characters, then this function will return NULL. So, this while loop will executed until fgets() is not equal to NULL.

if( len < strlen( str ) )

In this if statement, we are comparing the value of len and the length of str. If this condition is true, then we will perform the next two operations.

i. strcpy( longest, str );

This function copies the contents of str to the longest. This function will be executed only when the length of previous string is less than length of current string.

ii. len = strlen( str );

Now, we are updating the the value of the length. Because, we have to print the longest line so every time we have to check and update the value of the variable len.

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

printf(” Longest Line is %s “, longest );

If we have reached to this line, that means we have got our longest line. So, this printf will print the longest line in a file.

In this way, you can find the longest line in a file using a c program. I hope you have understood this program. If you have any difficulty, then please, feel free to contact with me.

Thank you.

Leave a Comment