Write a C program to read a file and display its contents on console. How to read a file and display file contents on console in C programming. In this exercises, I will explain you how to read a file character by character using fgetc()
. How to read a file line by line using fgets()
.
Required knowledge
Basic Input Output, Do while loop, While loop, Pointers, File Handling
In the previous post, I explained how to use FILE
pointer, fopen()
, fputs()
and fclose()
function to create and write data into file.
In this post, we will continue further and will learn various functions in C programming to read and display file contents on console.
How to read data from a file?
C programming supports four predefined functions fgetc()
, fgets()
, fscanf()
and fread()
to read data from file. These functions are defined in stdio.h
header file.
fgetc()
– Used to read single character from file.
fgets()
– Used to read string from file.
fscanf()
– Use this to read formatted input from file.
fread()
– Read block of raw bytes from file. Used to read binary files.
Step by step descriptive logic to read a file and display file contents.
- Open a file using
fopen()
function and store its reference in aFILE
pointer say fPtr.You must open file in r(read) mode or atleast mode that support read access.
- Read content from file using any of these functions
fgetc()
,fgets()
,fscanf()
orfread()
. - Finally, close the file using
fclose(fPtr)
.
In this post I will explain how to read a file using fgetc()
and fgets()
. I will explain separately how to read formatted input and binary files.
How to read a file character by character using fgetc()?
- The function accepts pointer to
FILE
type, source stream to read. - On each successful read it return character (ASCII value) read from stream and advance read position to next character.
It returns a constant EOF (-1) on unsuccessful read or if there is no more content to read.
Program to read a file character by character using fgetc()?
Suppose data/file1.txt
contains
Output
File opened successfully. Reading file contents character by character. Hurray!!! I learned to create file in C programming. I also learned to write contents to file. Next, I will learn to read contents from file on Codeforwin. Happy coding ;)
How to read a file line by line using fgets()?
- It accepts three parameters among which str is pointer to character. On success str points to string read from given stream.
- Next, num is maximum characters to read from stream.
- stream is a pointer to
FILE
type specifying input stream to read. - On success the function sets str to point to character read and return the same str. On failure it sets str to
NULL
pointer and returnNULL
pointer.
Program to read a file line by line using fgets()
Suppose data/file2.txt
contains
Output
File opened successfully. Reading file contents line by line. Reading a file line by line. -------------------------------------------- I love programming in C. Learning programming on Codeforwin is easy.
Recommended programs to practice
- File handling exercises index.
- C program to create a file and write data into file.
- C program to read numbers from a file and write even, odd and prime numbers in separate file.
- C program to append data into a file.
- C program to compare two files.
- C program to copy contents from one file to another file.
- C program to read and merge two files in single file.
Happy coding