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.
FILEstructure: C provides, in its standard library, a structure namedFILEfor handling files.stdio.h: The declaration of theFILEstructure is present instdio.h, sostdio.his 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 theFILEstructure is defined (for exampleFP). TheFILEstructure itself is never declared by the programmer.
How it works
- Include
stdio.hso that the declaration of theFILEstructure and all its functionalities are available. - Declare a pointer to the
FILEstructure, for exampleFILE *FP;. No declaration of theFILEstructure itself is required, since it is already done instdio.h. - 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.
FILEis a structure provided in the C standard library; its declaration is instdio.h.- Include
stdio.hbefore using theFILEstructure. - Declare only a pointer to
FILE(e.g.,FILE *FP;); never declare theFILEstructure 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.