File Input/Output
Files A collection of related data treated as a unit. Two types Text Binary Stored in secondary storage devices. Buffer Temporary storage area that holds data while they are being transferred to or from memory.
Text Files Data are stored as human-readable characters Each line of data ends with a newline character
Standard Files We have already discussed the standard files: Standard input Associated with the keyboard Accessed using stdin Standard output Associated with the monitor Accessed using stdout Standard error Accessed using stderr
User Files External files defined by the user Must be explicitly opened in the program. C assigns a logical name to each external file. fopen() function Prepares a file for processing Makes the connection between the external file and the program Creates a program file table or control structure to store the information needed to process the file
fopen FILE * fopen(char *filename, char *mode) filename – string that supplies the name of the file as known to the external world. e.g. scores.dat mode Meaning r Open file for reading If file exists, the marker is positioned at beginning. If file does not exist, error returned w Open text file for writing If file exists, it is emptied. If file does not exist, it is created. a Open text file for append. If file exists, the marker is positioned at the end.
Examples // Define and open the file scores.dat for input FILE *dataFile; dataFile = fopen("scores.dat", "r"); // Define and open the file prog.out for output FILE *outFile; outFile = fopen("prog.out", "w");
fopen() If succesful, will return a pointer to a FILE object If not, a null pointer is returned Always check to make sure the open was successful. If not, print an error message and exit Example: FILE *input = fopen("data.dat", "r"); if(input == NULL) { printf("Failure to open file. Exiting\n"); exit(1); }
fopen() This semester, we will use the assert() function Example: FILE *input = fopen("data.dat", "r"); assert(input != NULL); We will need to have #include <assert.h> at the top of our program.
fclose int fclose(FILE *fp) Note that the parameter is a FILE *, not the name of the file. Used to close a file when no longer needed Prevents associated file from being accessed again Guarantees that data stored in the stream buffer is written to the file Releases the FILE structure so that it can be used with another file Frees system resources, such as buffer space Returns zero on success, or EOF on failure
fclose() Examples: fclose(input); fclose(dataFile); fclose(outFile);
Field Specification Consists of % character, a conversion code, and other formatting instructions With one exception (*), each field specification must have a matching parameter in the parameter list that follows the string The type in the field specification and the type of the parameter must match Can have up to six elements for output Can have up to five elements for input; precision is not allowed
Field Specification Elements of field specification scanf / fscanf: % flag Max width size conversion code * do not store data h short l long int l double (scan only) L long double left justify + sign (+ or -) space if positive 0 zero padding d, f, c, s, e, E, x, X, i, u, g, G % flag Max width precision size conversion code printf / fprintf
Field Specification Examples %d %7d %+d %lf %7.2lf
End of File Controlled Loops feof int feof(FILE *fp) Function to check if end of file has been reached. For an end of file controlled loop Read before the loop Test for end of file: while (!feof(ptr)) Inside loop: Process Read at the bottom of the loop