Data Structures · File Handling
Opening a File with fopen and File Modes
Overview
To use a file in a C program, the file must first be opened with the library function fopen, which tells the system which file is wanted and how it will be used (read, write, or append). The "how" is called the mode.
Understanding fopen and its modes matters because the mode decides whether existing data is preserved, erased, or whether the open fails outright.
Key concepts
- Opening a file: the file must be opened before any operation on it; this is done with
fopen. - Mode: how the file is going to be used — reading, writing, or appending. Generally three modes exist.
fopenarguments: exactly two — the name of the file as a string, and the mode as a string.- File name: can be a plain name or a full path such as
/home/ravi, passed as a string. - Return value:
fopenreturns a pointer to the file structure, caught in the already-defined file pointerfp. After opening, all operations on the file go through this pointer. - Failure:
fopenmay returnNULLif it is unsuccessful.
How it works
- Decide which file to use and give its name (or full path) as a string.
- Decide the mode: reading, writing, or appending.
- Pass both strings to
fopenand catch the returned pointer in the file pointerfp. - If the open succeeds,
fppoints to the file structure and various operations can be performed on the file through it; if it fails,fpisNULL.
Formulas
fp = fopen(file_name, mode);
file_name= string containing the file name (or full path, e.g."/home/ravi")mode= string telling how the file is used (read / write / append)fp= file pointer (pointer to the file structure); becomesNULLif opening is unsuccessful
Worked examples
fp = fopen("filename", "mode");
The file name and mode are passed to fopen; the returned pointer to the file structure is caught in fp. The file is then open, and operations on it are performed depending on this pointer.
Comparison
| Situation | Behaviour |
|---|---|
| Open non-existing file in reading mode | Error; fopen returns NULL |
| Open existing file in writing mode | All existing information in the file is deleted; writing starts from the beginning |
| Open non-existing file in writing mode | A new file is created and its pointer is returned |
| Open existing file in append mode | Existing data stays as it is; new data is added |
Revision summary
- You cannot read from a file which does not exist; opening a non-existing file in reading mode gives an error.
- Always allow for
fopenreturningNULLon failure. - Writing mode on an existing file erases all its information and starts writing from the beginning.
- To keep the data inside the file as it is and only add to it, open it in append mode.
- Writing mode on a non-existing file creates a new file and returns its pointer.
fopentakes exactly two arguments, both strings: file name and mode.
Previous-year exam insights
- The standard C file modes are "r", "w", "a", plus their update forms "r+", "w+", "a+"; "r+" opens an existing file for both reading and writing without erasing it, "w+" truncates or creates for read/write, and "a+" appends and allows reading.
- Append mode ("a") always writes at the end of the file regardless of any seek; in "w" mode the file position starts at the beginning after truncation.
- Adding the letter "b" (e.g. "rb", "wb", "ab") opens the file in binary mode instead of text mode; on many systems text mode translates newline characters.
- fopen returns a FILE * (pointer to an opaque FILE structure), not a file descriptor; the underlying descriptor is obtained with fileno, and the file is closed with fclose(fp), which flushes buffers and returns 0 on success or EOF on failure.
- If fopen fails it returns NULL and sets errno; the cause can be printed with perror() or strerror(errno).
A NULL return must be checked before any read/write, otherwise the program crashes. - Opening a file in "w" or "w+" mode creates the file if it does not exist and truncates it to zero length if it does; "a" and "a+" also create a missing file but never truncate an existing one.