This chapter considers file processing using C's standard I/O family of functions. These functions include: fopen(), getc(), putc(), exit(), fclose(), fprintf(), fscanf(), fgets(), fputs(), rewind(), fseek(), ftell(), fflush(), ungetc(), setvbuf(), fread(), and fwrite(). The objectives important to this chapter include:
Concepts:A file is a named section of storage usually on a disk. The two ANSI-mandated views (not files but views) are text and binary. C automatically opens three files: stdin, stdout, and stderr. Standard input functions are getchar(), gets(), and scanf(), while standard output functions are putchar(), puts(), and printf(). Standard I/O is buffered. Information is transferred in large chunks instead of a byte at a time. The fopen() function is used to open a file. The standard fopen() modes are "r", "w", and "a", for text, and "rb", "wb", and "ab", for binary. The fopen() functions returns a pointer of type pointer to FILE. The fclose() function is used to close an open file. The exit() function closes all open files. File I/O functions include fprintf(), fscanf(), fgets(), and fputs(). While each is similar to their non-file counterparts, all require that you identify a file using a pointer to FILE. The general format of fprintf() and scanf() are: fprintf(fp, text, variables); scanf(FP, text, variables); In each case, FP is the pointer to FILE. The general format of fgets() and fputs() are: fgets(buf, MAX, FP); fputs(buf, FP); where buf is the name of a char array, MAX is the maximum size of the string, and FP is the pointer-to-FILE. Random access processing needs additional functions to move to a specific location in a file. The fseek() function enables you to treat a file like an array and move directly to any particular byte in a file opened by fopen(). The ftell() function returns the current position in a file as a long value. The format of fseek() is: fseek(FP, offset, mode); where FP is a pointer-to-FILE, offset is how far from the starting point to move, and mode is where to measure the offset from. The three modes are: SEEK_SET, SEEK_CUR, AND SEEK_END. The ftell() function may work differently in the text mode than in the binary mode. For this reason, a programmer is better off using the binary mode rather than the text mode for random-access programs. Other standard I/O functions important to this chapter are:
Binary read and write functions are performed by fread() and fwrite(). The function prototypes for these functions are: size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *FP) size_t fread(void *ptr, size_t size, size_t nmemb, FILE *FP) where size_t is the type returned by the sizeof operator, pointer to void is a catchall type for pointers, nmemb is the number of members, and *FP is a pointer to FILE. Two last functions are feof() and ferror(). The feof() function returns a nonzero value if the end-of-file has been detected. The ferror() function works like feof(), except it returns a nonzero value if a read or write error occurs. |