C Program to Read a File

In this article, you will learn and get code about reading of a file present inside the current directory. Current directory means, the directory where you are saving your C source code or programs.

Things to Do before Program

Before going through the program, we must have to create a file say codescracker.txt with some content inside it and save this file to the current directory. For example, put the following content:

Hello C
This is a textual file
The name of this file is codescracker.txt

in codescracker.txt file. This file is saved inside the c programs folder of Documents directory present inside the C-drive of my computer. Here is the snapshot of this folder:

read file c

As you can see, there is a file named codescracker.txt is available now in this folder. Let's move on to the program to read this file using C code.

Read a File in C

The question is, write a program in C to read a file. The answer to this question is:

int main()
    char fname[20], str[500];
    FILE *fp;
    printf("Enter the Name of File: ");
    fp = fopen(fname, "r");
        printf("Error Occurred while Opening the File!");
        fscanf(fp, "%[^\0]", str);
        printf("\nFile Read Operation Performed Successfully!");
    return 0;

To display the content of file after reading, refer to Display File Content or follow the Next Program after learning all the things given below.

This program was build and run under Code::Blocks IDE. If you are not saving your source code inside the folder where the file codescracker.txt is created as told at early of this article. Then either you can move that file to the directory where you are saving your source code, or save the above program in the directory where you have created your file named codescracker.txt.

Therefore, because I've created this file in c programs folder of Documents directory. Then follow the navigation (to save above program in the directory where the textual file is created):
File->Save file as..
you will see the following window in Code::Blocks:

c read file

Write the name of source code say codescracker.c and save it in the directory where you've create the textual file.

Now here is the sample run:

c program read file

Enter the name of file say codescracker.txt and press ENTER key to see the following output:

read a file program c

The program given above opens the file entered by user at run-time. The file gets opened in reading mode using r file opening mode. The function fopen() is used to open any file. It takes two argument. The first one is name of file and the second argument corresponds to file opening mode. It returns NULL, if file doesn't exist or anything strange happened while opening the file.

Note - The %[^\0] indicates, read the content of file until a null terminated character (\0) occurred.

Note - The size of str is initialized with 500 supposing that the file does not contain more than 500 characters

Same Program in Other Languages

C Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋