C Program to Read a File

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

Things to Do Before the Program

Before going through the program, we must create a file, say "fresherearth.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 fresherearth.txt

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

read file c

As you can see, there is a file named "fresherearth.txt" 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:

#include<stdio.h>
#include<conio.h>
int main()
{
    char fname[20], str[500];
    FILE *fp;
    printf("Enter the Name of File: ");
    gets(fname);
    fp = fopen(fname, "r");
    if(fp==NULL)
        printf("Error Occurred while Opening the File!");
    else
    {
        fscanf(fp, "%[^\0]", str);
        printf("\nFile Read Operation Performed Successfully!");
    }
    fclose(fp);
    getch();
    return 0;
}

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

This program was built and runs under the Code::Blocks IDE. If you do not save your source code in the folder where the file fresherearth.txt is created, as instructed at the start 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 fresherearth.txt.

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

c read file

Give the source code a name, such as fresherearth.c, and save it in the same directory where you saved the text file.

Now here is the sample run:

c program read file

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

read a file program c

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

Note: The %[^\0] indicates that you should read the content of the file until a null-terminated character (0) occurs.

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

The same program in different languages

C Quiz


« Previous Program Next Program »