1. Gyanastra
  2. Data Structures
  3. File Handling
  4. Opening a File with fopen and File Modes
Free lesson, no account needed 4 min read 8 practice questions after it

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.
  • fopen arguments: 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: fopen returns a pointer to the file structure, caught in the already-defined file pointer fp. After opening, all operations on the file go through this pointer.
  • Failure: fopen may return NULL if it is unsuccessful.

How it works

  1. Decide which file to use and give its name (or full path) as a string.
  2. Decide the mode: reading, writing, or appending.
  3. Pass both strings to fopen and catch the returned pointer in the file pointer fp.
  4. If the open succeeds, fp points to the file structure and various operations can be performed on the file through it; if it fails, fp is NULL.

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); becomes NULL if 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

SituationBehaviour
Open non-existing file in reading modeError; fopen returns NULL
Open existing file in writing modeAll existing information in the file is deleted; writing starts from the beginning
Open non-existing file in writing modeA new file is created and its pointer is returned
Open existing file in append modeExisting 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 fopen returning NULL on 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.
  • fopen takes 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.
Pages 1–2 of 2