Skip to content
Tuesday, December 1, 2026Ideas worth exploring. Knowledge worth sharing.
S. SenseCentralDISCOVER EVERY DAY
Programming & Data Structures

C Program to Read a Line From a File and Display it

In this example, you will learn to read text from a file and store it in a string until the newline ‘\n’ character is encountered.

To understand this example, you should have the knowledge of the following C programming topics:

·         C File Handling

·         C Programming Strings

Program to read text from a file

If the file is found, the program saves the content of the file to a string c until ‘\n’ newline is encountered.

Suppose the program.txt file contains the following text in the current directory.

C programming is awesome.

I love C programming.

How are you doing?

The output of the program will be:

Data from the file:

C programming is awesome.

If the file program.txt is not found, this program prints an error message.

Author

  • A senior editor for The Mars that left the company to join the team of SenseCentral as a news editor and content creator. An artist by nature who enjoys video games, guitars, action figures, cooking, painting, drawing and good music.

WRITTEN BY

Taylor Emma

A senior editor for The Mars that left the company to join the team of SenseCentral as a news editor and content creator. An artist by nature who enjoys video games, guitars, action figures, cooking, painting, drawing and good music.

More from this author ↗

Leave a Reply

Your email address will not be published. Required fields are marked *