Chapter 11 Structure and Union Types
Chapter 12 Text and Binary File Processing
True/False (每題5分)

1. If blob is a variable of a user-defined structure type, the following call to blob_fun could be passing blob as either an input or an output argument.

blob_fun(blob, num); [False]

2. The call to blob_fun shown in Question 1 causes the address of the structure blob to be passed to function blob_fun. [False]

3. A component of a structure can be another structure or an array, as well as a simple data type. [True]

4. To reference a component of a structured variable, one uses the variable name followed by a period followed by the component name. [True]

5. The direct component selection operator (.) has higher precedence than the address-of operator (&) and the indirection operator (*). [True]

6. A typedef statement allocates no memory. [True]

7. A C file name can be different from the name of the file pointer used for access. [True]

8. Function fprintf is used to write to a binary file, but not to a text file. [False]

9. In most operating systems, opening an existing file in "w" or "wb" mode causes the information previously in that file to be lost. [True]

10. Keyboard input is viewed by C as a text input stream. [True]

11. Function fopen returns the pointer NULL if it attempts to open a nonexistent input file. [True]

12. Function fwrite can only be used to write the contents of variables of C's standard types; it cannot be used with user-defined types. [False]

Multiple Choice (每題8分)

1. Which reference could mean

- Follow the pointer in stemp to a structure.

- Select the component named nums (this component is an array).

- Reference element 4 of the array.

a. stemp.nums[4]

b. *stemp.nums[4]

*c. (*stemp).nums[4]

d. *(stemp.nums)[4]

e. none of the above

2. Which statement below must be true if this call to printf is valid?

printf("%d", obj[2].len);

a. len is a structure component of type double.

*b. obj is an array of structures.

c. obj is a structure in which one component is an array.

d. len is the second component of structure obj.

e. None of these statements is necessarily true.

3. Consider the following function call:

fscanf(inp, "%d%lf", &x, &y)

Assuming that the data available in the file accessed through inp is

54 32.54

a. The value stored in x is 54.

b. The value stored in y is 32.54.

c. The value of the expression is 2.

d. a and b are true.

*e. a, b, and c are true.

4. Assuming that fp is a pointer to a nonempty binary file containing integers, that gp is a file pointer accessing an open output file that is a binary file, that the values in the file accessed by fp are arranged in nondecreasing order, and that stat, s, and r are type int variables, what does this program fragment do?

fread(&s, sizeof (int), 1, fp);

fwrite(&s, sizeof (int), 1, gp);

for (stat = fread(&r, sizeof (int), 1, fp);

stat == 1;

stat = fread(&r, sizeof (int), 1, fp))

if (r != s) {

s = r;

fwrite(&s, sizeof (int), 1, gp);

}

*a. Makes a copy of the file accessed by fp, removing duplicates.

b. Makes a copy of the file accessed by fp, writing only those entries of the file appearing twice or more in the original version.

c. Writes those integers from the file accessed by fp that do not also appear in the file accessed by gp.

d. Writes to the file accessed by gp two copies of each element of the file accessed by fp.

e. None of the above.

5. If bin_inp is a pointer to an open binary input file and list is a 100-element array of type double, what single statement will copy up to 100 values from the file into the array and will save in list_size the number of values copied?

a. list_size = fscanf(bin_inp, "100%lf", list);

b. fread(&list, &list_size, sizeof (double), 100, bin_inp);

c. fscanf(bin_inp, "%lf", &list, &list_size, 100);

*d. list_size = fread(list, sizeof (double), 100, bin_inp);

e. none of the above