Use a new datatype called a file pointer to with C files .
This type is written as FILE *, and defined within stdio.h.
A file pointer called output_file is declared in a statement is as follows:
A D V E R T I S E M E N T
Opening a file pointer using fopen:
Your program must open a file before it can access it. This is done using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will usually use fopen as follows:
if ((output_file = fopen("output_file", "w")) == NULL)
fprintf(stderr, "Cannot open %s\n", "output_file");
fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of:
r-open for reading
w-create file for writing
a-open file for appending
Abstractly, a file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. The collection of bytes may be interpreted, for example, as characetrs, words, lines, paragraphs and pages from a textual document; fields and records belonging to a database; or pixels from a graphical image. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file.
Closing a file using fclose:
The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.
This would be done using a statement like:
fclose(output_file);
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
perl functions,
c tutorial,
php functions,
c array,
c time,
c programs,
c examples,
c language,
c library,
c dll,
unix c,
c program,
c example,
standard c,
array functions,
file open,
variable functions,
unix file,
functions tutorial,
functions examples,
file read,
file write,
simple c,
sample c,
variable c,
functions example