Read File In C Programming

The C programming language provides different functions in order to read files. The file can be a binary file or a text file. The fgetc() , fgets() , fscanf() and fread() functions can be used to read files. In order to read a file, it should be opened with the fopen() function.

Open File

In order to read a file in C, we should first open the file. The file can be opened by using the fopen() which returns a FILE handle to be used by file read methods. The fopen() method requires at least two parameters. The first parameter is the file name which can also provide the file path. The second parameter is the mode that can be read, write, read+write, append etc. The syntax of the fopen() is like below.

fopen(FILE,MODE)
  • FILE is the file name or file name with path. It is provided as a string like /home/ismail/db.txt”.
  • MODE is used to set file open mode like reading, write, read+write, append, etc. For example, if the file is opened with the read mode it can not be written or appended.

In the following example, we open the file named “db.txt” with reading permissions.

#include <stdio.h>

main() {

   FILE *fp;

   fp = fopen("db.txt", "r");

   fclose(fp);

}

Every file open operation allocates some memory and system-related resources for file read operations. We should also close the opened file with the fclose() method by providing the file handle.

Read File with fgetc() Method

The fgetc() method is used to get a single character from the file. The file handle is provided to the fgetc() method and a single character is returned from the file. The cursor is iterated to the next character. We can use the fgetc() method in order to read the complete file by iterating every character with the do..while loop.

#include <stdio.h>

main() {

   FILE *fp;

   fp = fopen("db.txt", "r");

   do {
        ch = fgetc(fp);
        printf("%c", ch);
     } while (ch != EOF);

   fclose(fp);

}

Read File with fgets() Method

The fgets() method reads a string from the file for the specified size. The fgets() method sets its str parameter to the read string. We read 50 characters to the str in the following example. The while loop is used to iterate over all strings inside the file to the end. The NULL is used to check if the fgets() reached the end of the file.

#include <stdio.h>

main() {

   FILE *fp;


   char str[50];

   fp = fopen("db.txt", "r");

   while(fgets(str,50,fp)!=NULL){
      printf("%s",str)
   }
   fclose(fp);

}

Read File with fscanf() Method

The fscanf() function is created to read files in a formatted way. As expected we can read formatted files using the fscanf() function. The fscanf() method reads a single line in every execution. So we use the fscanf() method with the while loop.

#include <stdio.h>

main() {

   FILE *fp;

   char str[50];

   fp = fopen("db.txt", "r");

   while(fscanf(fp,"%*s %*s %s",str) ==1){
      printf("%s",str)
      prinf("%s\n",str)
   }
   fclose(fp);

}

Leave a Comment