C program to read the contents of a text file character by character

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 :

  1. Create one FILE pointer and one character variable ch. The file pointer will point to the file we will open and the character variable will hold the current reading character.
  2. Open the file in read mode. We are using 'r' for opening it on read mode. The fopen method 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 .c file with the above program is saved. You can also give a full path like c:\\Users\\username\\Desktop\\example.txt to this program.
  3. We are checking if the filePointer is not NULL. If the file doesn’t exist or the path is invalid, it will be NULL.
  4. With one while loop, we are reading the characters of the file one by one. We are using the fgetc method to read the characters of the file one by one. It will continue till the character is not EOF or until it reaches the end of the file. Inside the loop, we are using one printf statement to print out the current character.
  5. Finally, close the file using the fclose method.

Sample output:

This function will print out the content saved in the example.txt file. c program read file

As you can see, the content of the .txt file I have saved on my drive contains only Hello World !! text.

Download it on Github

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 input and output file pointers are used for the input and output files.
  • We are using the w mode to open the output file. It will create that file if it is not already available in the given path.
  • It uses the fputc method to write the characters of the input file.

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.

Download it on Github

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.