File Processing
Files and Streams
C views each
file simply as a sequential stream of bytes. Each file ends either with
an end-of-file markeror at a specific byte number recorded in a
system-maintained, administrative data structure.
When a file
is opened, a stream is associated with it.
treams provide communication channels
between files and programs. Three files and their associated streams are
automatically opened when program execution begins:
- the standard input to read data from the keyboard
- the standard output to print data on the screen.
- the standard error
File Definition
A file represents
a sequence of bytes on the disk where a group of related data is stored. File
is created for permanent storage of data. It is a ready made structure.
In C language, we use a
structure pointer of file type to declare a file.
Opening and Closing Files
The
fopen() function
is used to create a new file or to open an existing file.
General Syntax:
*fp=FILE*fopen(const char*filename,const char*mode);
Here,
*fp is
the FILE pointer (FILE *fp), which will hold the reference to the
opened(or created) file.
filename is the name of the file to be
opened and mode specifies the purpose of opening the file.
Mode can be of following types,
|
mode
|
description
|
|
r
|
opens a text file
in reading mode
|
|
w
|
opens or create a
text file in writing mode.
|
|
a
|
opens a text file
in append mode
|
|
r+
|
opens a text file
in both reading and writing mode
|
|
w+
|
opens a text file
in both reading and writing mode
|
|
a+
|
opens a text file
in both reading and writing mode
|
|
rb
|
opens a binary file
in reading mode
|
|
wb
|
opens or create a
binary file in writing mode
|
|
ab
|
opens a binary file
in append mode
|
|
rb+
|
opens a binary file
in both reading and writing mode
|
|
wb+
|
opens a binary file
in both reading and writing mode
|
|
ab+
|
opens a binary file
in both reading and writing mode
|
Closing a File
The
fclose() function
is used to close an already opened file.
General Syntax :
intfclose(FILE*fp);
Here
fclose() function
closes the file and returns zero on success, or EOF if
there is an error in closing the file. This EOF is a constant
defined in the header file stdio.h.
No comments:
Post a Comment