C Programming

File Opening Modes in C

File handling is one of the most important topics in the C language. In case of file handling, file opening modes play an important role in executing these programs. There are several types of modes available in file opening modes. Each of them will discuss in our article.

At first, we will see a programming example of how file handling can be done in the C language.

Programming Example 1

At first, we will see a programming example of how file handling can be done in the C language.

#include <stdio.h>

int main () {
    int i;
    FILE *fp;
    char s [100];
    fp = fopen ("f1.txt","w");      // file opening mode.

    if (fp == NULL) {
        printf (" File can not open. ");
        exit (1);
    }

    printf (" Enter a string: ");
    gets (s);       // string enters by the user.

    for (i=0; i<strlen (s); i++);    {

       fputc (s[i], fp);        // prints the each string.
    }

    fclose(fp);         // file is closed.
    return 0 ;
}

Output

Explanation

Here we explain this particular program with a diagram. Here we can show how the file is transferred and loaded, and handled in the memory.

Diagram: File Handling

If we want to write something on a file, that file exists in Hard disk name f1.text; then we have to open the file through fopen (); it makes an image of the file f1 in the RAM that is called Buffer. It is an intermediate location where we write when Buffer is full; it goes to automatic in the Hard Disk. If the buffer is not full, fclose () transfer our file into the Hard Disk.

Now we discuss different types of modes available in the C language.

File Opening Modes

Mode Meaning Description
r Read Only reading possible. No, create the file if it does not exist.
w Write Only writing is possible. Create the file if it does not exist; otherwise, erase the old content of the file and open a blank file.
a Append Only writing is possible. Create a file; if it does not exist, otherwise open the file and write from the end of the file. (Do not erase the old content).
r+ Reading + Writing Reading and writing are possible. Create a file if it does not exist, overwriting existing data. Used for modifying content.
w+ Reading + Writing Reading and writing are possible. Create a file if it does not exist. Erase old content.
a+ Reading + Appending Reading and writing are possible. Create a file if it does not exist. Append content at the end of the file.

Programming Example 2

This programming example shows us how a file is opened in read mode.

#include <stdio.h>

int main (){

    FILE *fp;
    fp= fopen (" myfile.dat " ," r ");      // file opening mode.
    if (fp == NULL)    {

        printf (" File cannot open. ");
    }
    return 0 ;
}

Output

Explanation

Here we open a file named “myfile.dat” in “r” mode. As I know from the file opening mode table, if we open a file in read mode, the file must exist. Otherwise, an error will have occurred. In this program, “myfile.dat” exists. For this, that file will open, and we will read the line from that file.

Programming Example 3

This programming example shows us how a file is opened in write mode.

#include <stdio.h>
#include <stdlib.h>

int main() {

  int n;
  FILE *fp;
  fp = fopen("write.txt","w");  // file opening mode.

  if (fp  ==  NULL) {
   
     printf (" file not found!! ");
     exit (1);
  }
  n = 2351;

  fprintf( fp,"%d", n);     // uses of fprintf() function.
  fclose (fp);      // file is closed.
  return 0;
}

Output

Explanation

Here we open a file named “write.txt” in “w” mode. As I know from the file opening mode table, only writing is possible if we open a file in write mode. If the file does not exist, this mode opens a new file, and writing is possible on that file. In this mode, erase all the old contents of the file.

Programming Example 4

This programming example shows us how a file is opened in append mode.

#include <stdio.h>
#include <string.h>

int main()
{
   FILE *fp;
   char st [100];

   fp = fopen ("data.txt", "a");        // file opening mode.

   printf ("Enter your message:");
   gets (st);

   fprintf (fp, "%s" ,st);

   printf (" Your message is appended in file. ");
   fclose (fp);     //close the file.

   return 0;
}

Output

Explanation

Here we open a file named “data.txt” in “a” mode. As I know from the file opening mode table, only writing is possible if we open a file in append mode. If the file does not exist, this mode opens a new file, and writing is possible on that file. In this mode, do not erase the old content.

Programming Example 5

This programming example shows us how a file is opened in “r+” mode. “r+” means reading + writing.

#include <stdio.h>
#include <stdlib.h>

int main ()
 {
   FILE * fp;
   char chr;
   fp= fopen ("data.txt"," r+ ") ;      // file opening mode.

   if (fp == NULL)   {

       printf ("file cannot open");
       exit (1);
   }

   fputs (" enter ",fp);
   fputs (" modify",fp);
   fclose (fp);     // close the file.
   return 0;
}

Output

Explanation

Here we open a file named “data.txt” in “r+” mode. As I know from the file opening mode table, if we open a file in reading + writing mode, create a file if the file does not exist. It overwrites the existing data. This mode is used for modifying the data content.

Programming Example 6

This programming example shows us how a file is opened in “w+” mode. “w+” means reading + writing is possible on the file.

#include <stdio.h>
#include <stdlib.h>

int main () {

   FILE * fp;

   fp = fopen (" myfile.txt ", " w+ ");     // file opening mode.
   fprintf (fp, "%s  %s %s %d", "Hello", "students", "of", 2004);

   fclose ( fp );       // close the file.

   return  0;
}

Output

Explanation

Here we open a file named “myfile.txt” in “w+” mode. As I know from the file opening mode table, if we open a file in reading + writing mode, create a file if the file does not exist. It erases the old content of the file.

Programming Example 7

This programming example shows us how a file is opened in “a+” mode. “a+” means reading + appending is possible on the file.

#include <stdio.h>
#include <stdlib.h>

int main () {

   FILE * fp;
   char chr;

   fp= fopen (" myfile.txt "," a+ ");       // file opening mode.
   if (fp == NULL)   {

       printf (" file does not exist ");
       exit (1);
   }

   fputs ("Good morning",fp);
   rewind(fp);

   while ( !feof (fp))   {

       chr=fgetc (fp);
       printf ( "%c ", chr);
   }

   fclose (fp);     // close the file.
   return 0 ;
}

Output

Explanation

Here we open a file named “myfile.txt” in “a+” mode. As I know from the file opening mode table, if we open a file in reading + appending mode, create a file if the file does not exist. It appends the content data at the end of the file.

Conclusion

We can see different types of file opening modes in the C language. Each opening mode has its own function and is used according to the program’s requirement. These mechanisms help the programmers to handle the files efficiently.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com