Answers to Chapter 15 Review Questions -------------------------------------- 1. A file is represented internally as a one-dimensional sequence of characters. 2. The process of opening a file consists of associating a file name with an internal file handle represented as a pointer to a FILE structure. 3. The type FILE * is uses to hold the information the system needs to keep track of an open data file. In most cases, the details of the underlying FILE structure are relevant only to the implementors of the file system and not to programmers who are simply using files as part of their application. 4. The second argument to fopen represents the mode in which the file is opened. The strings "r", "w", and "a" indicate reading from a file, writing to a file, and appending to a file respectively. 5. The fopen function returns failure by returning the pointer value NULL. 6. Closing files explicitly is good programming practice and makes it easier for other programmers to embed your code in new applications. 7. The stdio.h interface defines the standard files stdin, stdout, and stderr, which refer to the standard input file, the standard output file, and the standard error reporting file, respectively. In most environments, all three of these files refer to the console by default. 8. False, the function getc returns a value of type int, so that it can also represent EOF which is not a character. 9. The getc function returns the special sentinel value EOF to indicate the end of the input file. 10.The usual strategy for updating a file involves the following steps: a. Open the original file for input. b. Open a temporary file for input. c. Copy the input file to the temporary file, making any required editing changes in the process. d. Close both files. e. Delete the original file. f. Rename the temporary file so that it has the original name. 11. If you call rename(f1, f2), the file that used to be called f1 is given the new name f2. 12. The function ungetc returns a character to an input stream so that it can be read again by the next call to one of the input functions. The advantage of ungetc is that using it often simplifies the structure of an application by allowing it to, in essence, look ahead at the next character to decide what to do. 13. The typical call is fgets(buffer, bufSize, infile), where buffer is a character array large enough to hold the input line, bufSize is the allocated size of the buffer, and infile is the input file pointer. 15. The printf function always directs its output to the standard output device. The fprintf function has the same effect except that the output goes to a user-specified file pointer. The sprintf function writes its output to a string buffer. 16. True. 17. False. The arguments to scanf that indicate the destination for the various values must be pointers, but need not be specified explicitly using an ampersand. For string conversions in particular, the usual approach is to declare a character array and then use the array name, without an ampersand, in the scanf call. The array name is defined to be a pointer to the first element and therefore satisfies the rule that the argument must be a pointer. 18. White space characters are those for which the isspace predicate function in ctype.h returns TRUE. 19. A white space character in a scanf control string instructs scanf to skip over any white space characters in the input. 20. The specification %f is incompatible with the type of the variable d. The correct scanf call is: scanf("%d, %lf", &i, &d); 22. False. As developer P.J. Plaugher reports, the "usefulness [of scanf], over the years, has proved to be limited."