Reading File in C

1. Reading text files in C

Below is the C program to read the contents from the file:

C
// C program to read contents
// from the file
#include <stdio.h>
#include <stdlib.h>

// Driver code
int main()
{
  char str[80];
  FILE* ptr;

  ptr = fopen("Hello.txt", "r");

  if (ptr == NULL) 
  {
    printf("Error While opening file");
        
    // if the pointer returns NULL 
    // program will exit
    exit(1);
  }
  
  if(fgets(str, 80, ptr) != NULL)
  {
    puts(str);
  }
  fclose(ptr);

  return 0;
}


Output:

The output of Reading text files in C

2. Reading from a Binary File

fread() function also takes four arguments that are similar to fwrite() function in C Programming.

Syntax:

fwrite(const void *ptr,size_of_elements,number_of_elements, FILE *a_file);

Below is the C program to read from a binary file:

C
// C Program to Read from a 
// binary file using fread()
#include <stdio.h>
#include <stdlib.h>
struct Num 
{
    int n1, n2;
};

// Driver code
int main()
{
  int n;
  struct Num obj;
  FILE* fptr;
  if ((fptr = fopen("temp.bin", "rb")) == NULL) 
  {
    printf("Error! opening file");
        
    // If file pointer will return NULL
    // Program will exit.
    exit(1);
  }
    
  // else it will return a pointer 
  // to the file.
  for (n = 1; n < 10; ++n) 
  {
    fread(&obj, sizeof(struct Num), 1, fptr);
    printf("n1: %d\tn2: %d\n", obj.n1, obj.n2);
  }
  
  fclose(fptr);
  return 0;
}


Output:

The output of Reading from a Binary File

Explanation: In the above program, we have read the same file GFG.bin and are looping through records one by one. We read a single Num record of Num size from the file pointed by *fptr into the structure Num. We’ll get the same record that we inserted in the previous program. 

C – File I/O

In this article, we will learn how to operate over files using a C program. A single C file can read, write, move, and create files in our computer easily using a few functions and elements included in the C File I/O system. We can easily manipulate data in a file regardless of whether the file is a text file or a binary file using functions like fopen(), fclose(), fprintf(), fscanf(), getc(), putc(), getw(), fseek(), etc.

Similar Reads

What are files in C?

A file is used to store huge data. C provides multiple file management functions like file creation, opening and reading files, Writing to the file, and closing a file. The file is used to store relevant data and file handling in C is used to manipulate the data....

Types of Files in C

There are mainly two types of files that can be handled using File Handling in C as mentioned below:...

C Files Operations

C Files can perform multiple useful operations that are mentioned below:...

File Pointer declaration

For performing operations on the file, a special pointer called File pointer is used that can be declared as:...

File Modes in C

The second parameter i.e, “w” can be changed according to the table below:...

Opening a file in C

To perform the opening and creation of a file in c we can use the fopen() function which comes under stdio.h header file....

Creating a File in C

As now we know how to open a file using fopen() now the question arises about creation. The creation of a file is as simple as opening a file. As, if the while opening a file for writing or appending is done with either write(“w”) or append(“a”) mode then in the case where the file doesn’t exist a new file is created....

Writing a File in C

1. Writing to a text file in C...

Reading File in C

1. Reading text files in C...

Moving File Pointers to Specific Positions

fseek() and rewind() are the two methods in C programming that can be used to move the file pointer. Let us check both methods:...

FAQs on C File I/O

1. How to open a file in C?...