C Program to Read a Line from a File and Display it as Output
C program to read a line from a file and display it as output.
In this article, you will learn how to read lines from a given file step-by-step.
To solve this problem we'll take an input file named 'hello.txt'
with some sample paragraphs and open this file in the given C program.
INPUT FILE - `hello.txt`, contents in the input file
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.
You should know about the following topics in C programming to understand this program:
- C For loop
- C While loop
- C Pointers
- C File System
Source
// C Program to Read a Line from a File
#include <stdio.h>
#include <stdlib.h>
// It's the main function of the program
int main() {
// Step-1 Open a file named 'hello.txt' by creating a file pointer
FILE* file_string = fopen("./hello.txt", "r");
// Step-2 Buffer to store the each line of the opened file
char sentence[256];
// Step-3 Check if file didn't open
if (file_string == NULL) {
fprintf(stderr, "Unable to open file!\n");
}
// Step-4 If the file is opened then read each line of the opened file
else {
printf("Opened file string: 'hello.txt'\n");
printf("-----------------------------------------------------------------------\n\n");
// Iterate over each line of the file and store into 'sentence' Buffer
while (fgets(sentence, sizeof(sentence), file_string)) {
// Print each line of the file
printf("%s", sentence);
}
// Close the opened file when the file stream is completed
fclose(file_string);
printf("\n\n-----------------------------------------------------------------------\n");
}
return 0;
}
Output
Opened file string: 'hello.txt'
-----------------------------------------------------------------------
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable.
-----------------------------------------------------------------------
Explanation
- Created a file pointer
'file_string'
and opened the input file'hello.txt'
usingopen()
function. - Declared a buffer variable to store each line of the opened files.
- Condition added to check whether the file is successfully opened or not.
- If the file is successfully opened then start the stream of the opened file using
while loop
and storing each line of opened file. - Then, printed each line of the opened file.
- When streaming of the opened file is completed they close the opened file using
fclose()
function.