Program to Read File in C

How to Read File in C programming?

File handling is a very important function in C programming and is used to store large data which cannot be displayed due to limitation of the output console. There are various functions in File Handling but here we would learn about Reading File in C.

Reading-file-in-C

Reading File:

There are various way of reading file in C programming language and displaying it. They are:

  • fgetc()
  • fgets()
  • fscanf()
  • fread()

They all have different functions and are used according to their needs. Today we will learn how to reading a single line in C and displaying it.

Syntax:

A general code of reading file in C language.
int fscanf(FILE *ptr, const char *character, …) 
  • fscanf– it is the predefined function to read formatted data from the file and to store it in a variable.
  • ptr- it is the file pointer that pointes to the file from where data is being read from.

General Steps to Reading File in C:

  • Step 1: Start
  • Step 2: Declare the file pointer.
  • Step 3: Open the file in read mode.
  • Step 4: Check if the file is empty or not. 
  • Step 5:Read the file data and store it in a variable.
  • Step 6: Print the data on the stdout console.
  • Step 7: Close the file.
  • Step 8: Program end.

Working:

In these program we will learn to read file in C programming.

Using fgetc():

fgetc() function is used to read a single character from the file by the use of file pointer.

Example :

A simple program where we will read a file character by charatcer from the file using fgetc() function in File Handling.

Run
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    FILE* ptr;
    char ch;
    // Opening file in reading mode
    ptr = fopen("test.txt", "r");
    if (NULL == ptr)
    {
        printf("file can't be opened \n");
    }
    printf("content of this file are \n");
    // Printing what is written in file
    do 
    {
        ch = fgetc(ptr);
        printf("%c", ch);
        // Checking if character is not EOF.
    } while (ch != EOF);
    // Closing the file
    fclose(ptr);
    return 0;
}

Output:

File opened successfully

Using fgets():

fgets() is used to read a single string from an open file.

Example :

Program where we will read a file using fgets() function in File Handling.

Run
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    FILE* ptr;
    char str[50];
    ptr = fopen("test.txt", "a+");
    if (NULL == ptr) {
        printf("file can't be opened \n");
    }
    printf("content of this file are \n");
    while (fgets(str, 50, ptr) != NULL)
    {
        printf("%s", str);
    }
    fclose(ptr);
    return 0;
}

Input:

Previously stored data

Welcome to PrepInsta

Output:

Welcome to PrepInsta

Using fscanf():

It is used to read a single formatted string from an open file that is being pointed by the file pointer. 

Example :

Program where we will read a file using fscanf() function in File Handling.

Run
#include<stdio.h>
int main()
{
    FILE* pt = fopen("test.txt", "r");
    if (pt == NULL) {
        printf("no such file.");
        return 0;
    }
    char ch[100];
    while (fscanf(pt, "%*s %*s %s ",ch)== 1)
        printf("%s\n", ch);
    return 0;
}

Input:

Thank you for visiting PrepInsta

Output:

Thank you for visiting PrepInsta

Using fread():

It is used to read a single block of data from a file. It is the easiest form to read a data.

Example :

Program where we will read a file using fread() function in File Handling.

Run
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main ()
{
   FILE *fp;
   char c[] = "this is PrepInsta";
   char buffer[100];
   fp = fopen("file.txt", "r");
   /* Read and display data */
   fread(buffer, strlen(c)+1, 1, fp);
   printf("%s\n", buffer);
   fclose(fp);
   return(0);
}

Input:

Thank you for visiting PrepInsta

Output:

Thank you for visiting PrepInsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription