fopen() Function Usage In C and C++ with Examples – POFTUT

fopen() Function Usage In C and C++ with Examples


In C and C++ programming languages fopen() function is used to open files and make operations like add, update, create for data. In this tutorial we will learn the syntax, usage and errors about the fopen() function.

fopen() Function Syntax

fopen() function is provided by the standard C library. The fopen() function has the following syntax.

FILE *fopen(const char *FILENAME, const char *MODE)
  • `FILE *` is the return type of the fopen() function which is a pointer to a FILE structure. This file pointer is used for operations to add, update, etc.
  • `const char *FILENAME` is simply the file name that will be opened by fopen() function.
  • `const char *MODE` is the file open mode that will set the behavior of the file operations like only read, write, append, etc.

The fopen() function will return a FILE pointer to the opened file in successful execution. If there is error the return value will be NULL and the global variable errno will be set for the related error.

File Open Modes

Before starting the examples of the fopen() function we will learn file open modes. File open modes sets and restrict file access type like only read, update, create the file if do not exists, etc. File modes are represented as characters and + sign.

  • `”r”` is used as the file for reading. This will open the file as read-only and the file cannot be edited in this mode. As expected the file should exist where it will not be created automatically.
  • `”w”` will create an empty file for writing. If the file already exists the existing file will be deleted or erased and the new empty file will be used. Be cautious while using these options.
  • `”a”` is used for appending new data into the specified file. The file will be created if it doesn’t exist.
  • `”r+”` mode will open the file to update which will provide read and write rights. But the file must already exist if not it will not be created and throw an error.
  • `”w+”` will create an empty file for both reading and writing.
  • `”a+”` will open a file for reading and appending.
LEARN MORE  File Operations, Open, Write, and Close Files In C

Open File For Reading

We will start with a simple example where we will open a file named myfile.txt which is a text file as you expect. But keep in mind that the type or content of the file is not important for fopen() function.

#include <stdio.h>

int main () {
   //File pointer for "myfile.txt"
   FILE *fp;
   int c;

   //Open the file and set to pointer fp
   fp = fopen("myfile.txt","r");

   //Read file character by character and 
   //put to the standard ouput
   while(1) {
      c = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c", c);
   }

   //Close the file pointer
   fclose(fp);

   return(0);
}

We will compile the source code file read_myfile.c with the following gcccommand.

$ gcc read_myfile.c -o read_myfile

Open File For Writing

We can also use "a" for writing or appending into the existing file. In this example, we will add the following line file named myfile.txt.

I am new line
#include <stdio.h>

int main () {
   //File pointer for "myfile.txt"
   FILE *fp;
   int c;

   //Open the file and set to pointer fp
   fp = fopen("myfile.txt","a");

   //Add or append new line to myfile.txt
   // by using fp pointer
   fputs("I am a new line",fp);

   //Close the file pointer
   fclose(fp);

   return(0);
}

Close File

As fopen() function is used to open a file after the operations are completed we have to close the file. fclose() function is used to close the given file by using this file pointer and release resources. In previous examples as the last action, we closed the files like below.

fclose(fp);

fopen() Errors

While opening files with fopen() function we may get different types of errors. Here we will provide some of the most possible ones.

  • `EACCES` is related where there is no permission for the specified operation.
  • `EINVAL` will raise when the value of the mode argument is not valid.
  • `EINTR` will raise when a signal was caught during fopen().
LEARN MORE  What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java

Leave a Comment