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.

Related Posts

© 2024 Software Engineering - Theme by WPEnjoy · Powered by WordPress