UNIX C Programming

Chapter 12: File Input/Output



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:

  • Clarify the difference between text and binary files
  • Review the three standard files and buffered I/O
  • Explain the fopen(), getc(), putc(), and fclose() functions
  • Discuss the fprintf(), fscanf(), fgets(), and fputs() file I/O functions
  • Explain the use of fseek() and ftell() in random-access file processing
  • Review other standard I/0 functions: ungetc(), fflush(), and setvbuf()
  • Show how fread() and fwrite() differ from text file I/O
  • Clarify the fwrite(), fread(), feof(), and ferror() prototypes

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:

  • ugetc() Push the character specified by c back into the input stream
  • fflush() Flush the output buffer
  • setvbuf() Set up an alternative buffer to be for standard I/O functions

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.