|
FILE INPUT/OUTPUT
To work with files, the library routines must be included into your programs. This is done by the statement,
#include <stdio.h>
as the first statement of your program.
USING FILES
Declare a variable of type FILE
To use files in C programs, you must declare a file variable to use. This variable must be of type FILE, and be declared as a pointer type.
FILE is a predefined type. You declare a variable of this type as
FILE *in_file;
This declares infile to be a pointer to a file.
Associate the variable with a file using fopen()
Before using the variable, it is associated with a specific file by using the fopen() function, which accepts the pathname for the file and the access mode (like reading or writing).
in_file = fopen( "myfile.dat", "r" );
In this example, the file myfile.dat in the current directory is opened for read access.
Process the data in the file
Use the appropriate file routines to process the data
When finished processing the file, close it
Use the fclose() function to close the file.
fclose( in_file );
The following illustrates the fopen function, and adds testing to see if the file was opened successfully.
#include <stdio.h>
/* declares pointers to an input file, and the fopen function */
FILE *input_file, *fopen ();
/* the pointer of the input file is assigned the value returned from the fopen call. */
/* fopen tries to open a file called datain for read only. Note that */
/* "w" = write, and "a" = append. */
input_file = fopen("datain", "r");
/* The pointer is now checked. If the file was opened, it will point to the first */
/* character of the file. If not, it will contain a NULL or 0. */
if( input_file == NULL ) {
printf("*** datain could not be opened.\n");
printf("returning to dos.\n");
exit(1);
}
NOTE: Consider the following statement, which combines the opening of the file and its test to see if it was successfully opened into a single statement.
if(( input_file = fopen ("datain", "r" )) == NULL ) {
printf("*** datain could not be opened.\n");
printf("returning to dos.\n");
exit(1);
}
INPUTTING/OUTPUTTING SINGLE CHARACTERS
Single characters may be read/written with files by use of the two functions, getc(), and putc().
int ch;
ch = getc( input_file ); /* assigns character to ch */
The getc() also returns the value EOF (end of file), so
while( (ch = getc( input_file )) != EOF )
......................
NOTE that the putc/getc are similar to getchar/putchar except that arguments are supplied specifying the I/O device.
putc('\n', output_file ); /* writes a newline to output file */
CLOSING FILES
When the operations on a file are completed, it is closed before the program terminates. This allows the operating system to cleanup any resources or buffers associated with the file. The fclose() function is used to close the file and flush any buffers associated with the file.
fclose( input_file );
fclose( output_file );
COPYING A FILE
The following demonstrates copying one file to another using the functions we have just covered.
#include <stdio.h>
main() /* FCOPY.C */
{
char in_name[25], out_name[25];
FILE *in_file, *out_file, *fopen ();
int c;
printf("File to be copied:\n");
scanf("%24s", in_name);
printf("Output filename:\n");
scanf("%24s", out_name);
in_file = fopen ( in_name, "r");
if( in_file == NULL )
printf("Cannot open %s for reading.\n", in_name);
else {
out_file = fopen (out_name, "w");
if( out_file == NULL )
printf("Can't open %s for writing.\n",out_name);
else {
while( (c = getc( in_file)) != EOF )
putc (c, out_file);
putc (c, out_file); /* copy EOF */
printf("File has been copied.\n");
fclose (out_file);
}
fclose (in_file);
}
}
|