C program to read a file

devquora
devquora

Posted On: Dec 15, 2020

C program to read a file

 

Overview:

In this article, I have described the process of opening and reading a text file using file handling. Here, I will introduce you to a file and the concept to read a file. After that, I will show a Demonstration to read a file. At last, I will show a C program to read a file with output.

Table of contents:

  1. What is a File?
  2. How to read a file in C programming?
  3. Logic to read a file
  4. C program to read a file
  5. Conclusion

What is a File?

The file is a collection of bytes that are stored on secondary storage devices like the disk. There are two kinds of files in a system. There are basically two types of files as follows:

  1. Text files (ASCII)
  2. Binary files

Text files contain ASCII codes of digits, alphabetic, and symbols.

The binary file contains a collection of bytes (0’s and 1’s). Binary files are compiled versions of text files.

How to read a file in C programming?

The file to be opened must be present in the directory in which the executable file of this program is present. To read the file, we must open it first using any of the modes, for example, if you only want to read the file then open it in “r” mode. Based on the mode selected during file opening, we are allowed to perform certain operations on the file. For example, if you open a file in “r” mode, you won’t be able to write the file as “r” is the read-only mode that only allows reading. A read operation reads the structure where the file position indicator is pointing to. After reading the structure the pointer is moved to point at the next structure.

Logic to read a file

    For reading a file first of all you should open a file after all you will read that file. Now, Here, I will first write the logic to open a file as follows:
  • Step 1: Declare variable and file pointer
  • Step 2: Assign file pointer to fopen() function with write format
  • Step 3: If the file is not opened, the print error message
  • Step 4: Else give the content using the loop
  • Step 5: Close the file
    After that, I will write the logic For Reading a Text File as follows:
  • Step 1:Declare variables and the file pointer
  • Step 2:Open the file
  • Step 3:If the file is not opened print error message
  • Step 4:Print the content of the text file using the loop
  • Step 5:Close the file

C program to read a file

#include <stdio.h>
int main()
 {
    FILE *fp; // declaration of file pointer
    char con[1000]; // variable to read the content
    fp =fopen("file.txt","r");// opening of file
    if (!fp)// checking for error
    return 1;
    while (fgets(con,1000, fp)!=NULL)// reading file content
    printf("%s",con);
    fclose(fp); // closing file
    return 0;
 }

The output of the above Program:

When this Program successfully executed then the output of the file.txt is shown as follows:

#include <stdio.h>
int main()
 {
    FILE *fp; // declaration of file pointer
    char con[1000]; // variable to read the content
    fp =fopen("file.txt","r");// opening of file
    if (!fp)// checking for error
    return 1;
    while (fgets(con,1000, fp)!=NULL)// reading file content
    printf("%s",con);
    fclose(fp); // closing file
    return 0;
 }

C program to read a file output

Explanation of output:

Here con represents the string (array of char) in which you are storing the string after reading it from the file. 1000 is the length of the string that needs to be read every time. fp is a pointer to the file, which is going to be read. Why did I use if(fgets(con, 1000, fp)==NULL as logic to determine the end of the file? In the above examples, we have used ch==EOF to get to know the end of the file. Here we have used this logic because fgets returns NULL when there are no more records available to be read.

Conclusion:

The working mechanism of this source code is similar to the above source code. But, it uses the function fopen() to open the file.txt file in reading mode i.e. function is called with “file.txt” and “r” as the argument. After the file is opened in reading mode, the content of file.txt is displayed on the screen, and the file is closed.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..