The buffer area is established by writing
FILE *ptvar;
where FILE (uppercase letters required) is a special structure type that establishes the buffer area, 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 fopen is used to open a file.This function is typically written as
ptvar = fopen (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 in the following table.
File-Type | Meaning |
"r" | Open an existing file for reading only. |
"w" | Open a new file for writing only. 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 appending (i.e., for adding the information at the end of the file). A new file will be created if the file with the specified file-name does not exist. |
"r+" | Open an existing file for both reading and writing. |
"w+" | Open a new file for both reading and writing. If a file with the specified file-name currently exists, it will be destroyed and a new file will be 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 exist. |
Finally a file must be closed at the end of the program.This can be acomplished with the library function fclose. The syntax is simply
fclose (ptvar);
It is good programming practice to close a data file explicitly using the fclose function, though most C compilers will automatically close a data file at the end of a program execution.
No comments:
Post a Comment