Saturday, May 10, 2008

Opening & closing a data file

Opening & closing a data file :

When working with a stream-oriented data file, the first step is to establish a buffer area (a holt station for data processing) where information is temporarily stored while being transferred between the computer's memory and
the data file.

This buffer area allows information to be read from or written to
the data file more readily than would otherwise be possible. The buffer area is established by writing

FILE * ptvar;

Where FILE (uppercase letter required) is a special
structure type that establishes the buffer area, and ptvar
is a pointer variable that indicates the beginning of the buffer area.

The structure type FILE is defined within a system include file, typically stdio.h. The pointer ptvar is often referred to as a stream pointer, or simply a stream.

A data file must be opened before it can be created or processed. This associates the file name with the buffer
area (i.e., with the stream).

It also specifies how the data file will be utilized, i.e.,
as a read-only file, a write -only file, or a read/write
file, in which both operations are permitted.

The library function open is used to open a file. This
function is typically written as Ptvar = open (file-name, file-type)

Where file-name and file-type are strings that represent
the name of the data file and the manner in which the data
file will be utilized.

The name chosen for the file-name must be consistent with
the rules for naming files, as determined by the computer's operating system.

The file-type must be one of the strings shown:

"r" Open an existing file for reading only.
"w" Open a new file for writing only. If a file with the
specified filename currently exists, it will be destroyed
and a new file will be created in its place.
"a" Open an existing file for appending. A new file will
be created if the file with the specified file-name don't exists.
"r+" open an existing file for both reading and writing
"w+" Open an existing file for both reading and writing.
If a file with the specified file name currently exists,
it will be destroyed and a new file created in its place
"a+" Open an existing file for both reading and appending.
A new file will be created if the file with the specified
file-name does not exists.

0 comments:

Copyright

Copyright © 2008 C Tutorial, All rights are reserved