File Input/Output

Mao

Outline

● Functions: fopen() , getc() , putc() , exit() , fclose()

fprintf() , fscanf(), fread() , fwrite()

● How to process files using C’s standard I/O family of functions ● Text modes and binary modes

What Is a File?

A file is a named section of storage, usually on a disk, or, more recently, on a solid-state device.

You think of stdio.h , for instance, as the name of a file containing some useful information.

The Text Mode and the Binary Mode

All file content is in binary form (zeros and ones).

● If a file primarily uses the binary codes for characters (for instance, ASCII or Unicode) to represent text, much as a C string does, then it is a text file; it has text content.

● If, instead, the binary values in the file represent machine language code or numeric data (using the same internal representation as, say, used for long or double values) or image or music encoding, the content is binary.

The Text Mode and the Binary Mode

Unix uses the same file format for both kinds of content.

A C text-mode program compiled on an MS-DOS platform would convert \r\n to \n when reading from a file and convert \n to \r\n when writing to a file.

Standard Files and I/O

C programs automatically open three files on your behalf. They are termed

● the standard input , ● the standard output , ● and the standard error output .

The standard I/O package has many specialized functions that simplify handling different I/O problems. Input and output are buffered. That is, information is transferred in large chunks (typically 512 bytes at a time or more) instead of a byte at a time. A buffer is an intermediate storage area. This buffering greatly increases the data transfer rate.

1. the program in Listing 13.1 checks the value of argc to see if there is a command-line argument. If there isn’t, the program prints a usage message and exits.

The string argv[0] is the name of the program.

2. Next, the program uses fopen() to open the file. This function is declared in stdio.h . Its first argument is the name of the file to be opened; more exactly, it is the address of a string containing that name. The second argument is a string identifying the mode in which the file is to be opened.

3. The two functions getc() and putc() work very much like getchar() and putchar() . The difference is that you must tell these newcomers which file to use.

4. A program reading data from a file needs to stop when it reaches the end of the file. The getc() function returns the special value EOF if it tries to read a character and discovers it has reached the end of the file.

To avoid problems attempting to read an empty file, you should use an entry-condition loop (not a do while loop) for file input.

The fclose() Function

The fclose(fp) function closes the file identified by fp , flushing buffers as needed. For a program less casual than this one, you would check to see whether the file had been closed successfully. The function fclose() returns a value of 0 if successful, and EOF if not:

if (fclose(fp) != 0)

printf("Error in closing file %s\n", argv[1]);

The fclose() function can fail if, for example, the disk is full, a removable storage device has been removed, or there has been an I/O error.

Pointers to the Standard Files

The stdio.h file associates three file pointers with the three standard files automatically opened by C programs:

These pointers are all type pointer-to-FILE, so they can be used as arguments to the standard I/O functions, just as fp was in the example.

The fprintf() function is like printf() , except that it requires a file pointer as its first argument. We’ve used the stderr pointer to send error messages to the standard error; this is a standard C practice.

The file I/O functions fprintf() and fscanf() work just like printf() and scanf() , except that they require an additional first argument to identify the proper file.

The fgets() and fputs() Functions

The first argument of fgets() and fputs(), as with the banished gets(), is the address (type char *) where input should be stored. The second argument is an integer representing the maximum size of the input string. The final argument is the file pointer identifying the file to be read. A function call, then, looks like this:

fgets(buf, STLEN, fp);

Here, buf is the name of a char array, STLEN is the maximum size of the string, and fp is the pointer-to-FILE .

The fgets() and fputs() Functions

The fgets() function reads input through the first newline character, until one fewer than the upper limit of characters is read, or until the end-of-file is found; fgets() then adds a terminating null character to form a string.

The fputs() function takes two arguments: first, an address of a string and then a file pointer. It writes the string found at the pointed-to location into the indicated file. Unlike puts(), fputs() does not append a newline when it prints. A function call looks like this:

fputs(buf, fp);

Here, buf is the string address, and fp identifies the target file.

fwrite():

fread():

fwrite() writes count of objects from the given array buffer to the output stream:

count if an error occurs.

● If size or count is zero, fwrite returns zero and performs no other

action.

fread() reads up to count objects into the array buffer from the given input stream and stores the results:

an error or end-of-file condition occurs.

● If size or count is zero, fread returns zero and performs no other action. fread does not distinguish between end-of-file and error, and callers must use feof and ferror to determine which occurred.

Reference

● Stephen Prata. C Primer Plus (6th Edition). Addison-Wesley

Professional, 2013.