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 * 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  C - File Operations, Open, Write and Close Files

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.

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

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.

echo '
'

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.

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().

Leave a Reply

Your email address will not be published. Required fields are marked *