C program to read the contents of a text file character by character :
In this tutorial, we will learn how to read the contents of a text file in C programming language. You can modify the program to read the contents from multiple files if you want. This post will show you:
- How to read the contents of a text file character by character and print it on the console.
- How to read the contents of one text file character by character and write it to another text file.
Example 1: C program to read the contents of a text file:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// 1
FILE *filePointer;
char ch;
// 2
filePointer = fopen("example.txt", "r");
// 3
if (filePointer == NULL)
{
printf("File is not available \n");
return 0;
}
else
{
// 4
while ((ch = fgetc(filePointer)) != EOF)
{
printf("%c", ch);
}
}
// 5
fclose(filePointer);
return 0;
}Explanation :
The commented numbers in the above program denote the step number below :
- Create one
FILEpointer and one character variablech. The file pointer will point to the file we will open and the character variable will hold the current reading character. - Open the file in read mode. We are using
'r'for opening it on read mode. Thefopenmethod will return the pointer to the file if it is available. The file name is example.txt and it is in the same folder where the.cfile with the above program is saved. You can also give a full path likec:\\Users\\username\\Desktop\\example.txtto this program. - We are checking if the
filePointeris notNULL. If the file doesn’t exist or the path is invalid, it will beNULL. - With one
whileloop, we are reading the characters of the file one by one. We are using thefgetcmethod to read the characters of the file one by one. It will continue till the character is notEOFor until it reaches the end of the file. Inside the loop, we are using oneprintfstatement to print out the current character. - Finally, close the file using the
fclosemethod.
Sample output:
This function will print out the content saved in the example.txt file.
As you can see, the content of the .txt file I have saved on my drive contains only Hello World !! text.
Example 2: C program to read the contents of a text file and write to a different text file:
We can update the above program to copy the content of one text file to another. We need to open another file in write mode, w, and we can use the fputc method to write the characters to that file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *input;
FILE *output;
char ch;
input = fopen("example.txt", "r");
output = fopen("output.txt", "w");
if (input == NULL)
{
printf("File is not available \n");
return 0;
}
else
{
while ((ch = fgetc(input)) != EOF)
{
fputc(ch, output);
}
}
fclose(input);
fclose(output);
return 0;
}- The
inputandoutputfile pointers are used for the input and output files. - We are using the
wmode to open the output file. It will create that file if it is not already available in the given path. - It uses the
fputcmethod to write the characters of theinputfile.
Note that it will replace the content of the output file with the new content. If you want to append the new content to this file, you can use a mode with the fopen method.
Conclusion :
In this quick tutorial, we have learned how to read the contents of a file character by character in C programming. We also learned how to copy the content of one text file to another text file in C.

