Chapter 12 Files Management in C
Chapter 12 Files Management in C
1
12.1 Introduction
The console oriented I/O operations pose
two major problems:
1. It becomes cumbersome and time consuming to
handle large volumes of data through terminals.
2. The entire data is lost when either the program
is terminated or the computer is turned off.
To employ the concept of file to store data
A file is a place on the disk where a group
of related data is stored.
Functions to Perform Basic File
Operation
naming a file
opening a file
reading data from a file
writing data to a file
closing a file
High Level I/O Functions
Function Operation
fopen() *Create a new file for use
*Opens an existing file for use
fclose() *Closes a file which has been opened for use
getc() *Reads a character from a file
puts() *Writes a character to a file
fprintf() *Writes a set of data values to a file
fscanf () *Reads a set of data values from a file
getw() *Reads an integer form a file
putw() *Writes an integer to a file
fseek() *Sets the position to a desired point in the file
ftell() *Gives the current position in the file
rewind() *Sets the position to the beginning of the file
12.2 Defining and Opening a
File
The certain things about the file to the
operation system include:
– 1. Filename
– 2. Data structure
– 3. Purpose
File Name
File name is a string of characters that
make up a valid filename for the operating
system. It may contain two parts, a
primary name and an optional period with
the extension.
Example:
– input.data
– store
– Student.c
– Text.out
Data Structure
Data structure of a file is defined as FILE
in the library of standard I/O function
Declares the variable fp
definitions.
as a “pointer to the data
Generaltype
format
FILE”.for declaring and opening a
file:
FILE *fp;
fp = fopen(“filename”, “mode”);
FILE is a structure
that is defined in the Note that both the
I/O library filename and mode are
specified as strings
The Mode to Specify the Purpose
of Opening This File:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding)
data to it.
When trying to open a file, one of
the following things may happen:
1. When the mode is ‘writing’, a file with the
specified name is created if the file does not
exist. The contents are deleted, if the file
already exists.
2. When the purpose is ‘appending’, the file is
opened with the current contents safe. A file
with the specified name is created if the file
does not exist.
3. If the purpose is ‘reading’, and if it exists,
then the file is opened with the current
contents safe otherwise an error occurs.
Example
FILE *p1, *p2;
p1 = fopen(“data”, “r”);
p2 = fopen(“results”, “w”);
If results already exists, its contents are
deleted and the file is opened as a new
file.
If data file does not exist an error will
occur.
Additional Modes of Operation
* r+ The existing file is opened to the
beginning for both reading and writing.
* w+ Same as w except both for reading and
writing
* a+ Same as a except both for reading and
writing.