1. Gyanastra
  2. Data Structures
  3. File Handling
  4. Need for File Input and Output and the FILE Structure
Free lesson, no account needed 3 min read 2 practice questions after it

Data Structures · File Handling

Need for File Input and Output and the FILE Structure

Overview

Real programs do not normally read from the keyboard or print to the screen; they take input from files and write output to files, such as log files that record everything that happens during execution.
C supports this through the predefined FILE structure declared in stdio.h, and a program uses it by declaring a pointer to it.

Key concepts

  • File input/output in industry: Programs generally do not print to the screen and do not read from the user; the input is taken from a file and the output is written to a file.
  • Log file: Everything that happens during the execution of a program is noted in a log file instead of being printed on the screen all the time.
    When a user wants to see what happened, he opens the log file and reads it; alternatively, another program may open that log file and read it.
  • Required file operations: open a file, read from it, write into it, create a file, and append something to the end of a file.
  • FILE structure: C provides, in its standard library, a structure named FILE for handling files.
  • stdio.h: The declaration of the FILE structure is present in stdio.h, so stdio.h is included before the program uses it, and all the functionalities of the structure are already declared.
  • Pointer to FILE: Before opening any file, a pointer to the FILE structure is defined (for example FP). The FILE structure itself is never declared by the programmer.

How it works

  1. Include stdio.h so that the declaration of the FILE structure and all its functionalities are available.
  2. Declare a pointer to the FILE structure, for example FILE *FP;. No declaration of the FILE structure itself is required, since it is already done in stdio.h.
  3. Once the pointer is defined, the file is opened for use with fopen.

Worked examples

Declaring the pointer before opening a file:

#include <stdio.h>

FILE *FP;

FP is a pointer to the FILE structure which is already predefined in stdio.h; the structure itself is not declared in the program.

Revision summary

  • Screen and keyboard I/O is not the norm in industry products; input comes from a file and output goes into a file.
  • A log file records everything that happens during program execution and is read later by a user or by another program.
  • The operations to know are: open a file, read from it, write into it, create a file, append to the end of a file.
  • FILE is a structure provided in the C standard library; its declaration is in stdio.h.
  • Include stdio.h before using the FILE structure.
  • Declare only a pointer to FILE (e.g., FILE *FP;); never declare the FILE structure yourself.

Previous-year exam insights

  • fopen returns a FILE * that must be checked against NULL before use; a failed open (missing file, bad path, no permission) returns NULL, and dereferencing it is undefined behaviour.
  • The standard file modes are "r" (read, file must exist), "w" (write, truncates or creates), "a" (append, creates if absent), "r+", "w+", "a+" (update modes), optionally with "b" for binary; the mode string determines whether the file is truncated, created, or must already exist.
  • Every opened file must be closed with fclose(fp), which flushes buffered output and releases the stream; failing to close can lose buffered data.
  • Standard streams stdin, stdout and stderr are predefined FILE * values available without fopen, and stderr is unbuffered by default.
  • The FILE structure is an opaque object: the programmer only manipulates FILE * and must never rely on its internal fields or copy it; its layout is implementation-defined.
  • Common stream functions: fopen/fclose, fgetc/fputc, fgets/fputs, fscanf/fprintf, fread/fwrite, fseek/ftell/rewind, feof/ferror; EOF is returned by input functions at end of file or on error.
Pages 1–2 of 2