/* * SelfTest * Chapter 12 * By John Stile */ 1. What are the statements for declaring a file pointer named fin_num, opening a file named xyz for reading, and assigning it's file pointer to fin_num? ANSWER: FILE * fin_num; fin_num=(fopen("xyz", "r"); 2. What is the fprintf statement for writing "Hello World" to a file which has the file pointer out_file? ANSWER: fprintf( out_file, "%s", "Hello World" ); 3. How do the arguments of: getchar() gets() scanf() putchar() puts() printf() differ from their file equivalents: fgetc() fgets() fscanf() fputc() fputs() fprintf() ANSWER: Each takes a file pointer rather than stdout 4. (6 in the book) Which mode-constant should b e used in the fopen() function to open a binary file for reading only? ANSWER: fp=fopen("file_name", "rb" ); 5. (7 in the boot) Which statement writes the array of floats called employeeData to a file? Assume file is referenced through the file ponter fp, and the array size is 40. ANSWER: fprintf( &employeeData[0], sizeof(float), 40, fp) 6. (8 in the book) Which statement access the beginning of a binary file using random access? Assume the file is referenced through file pointer fp. ANSWER: fp=fopen("myfile", "r+b" ) 7. (9 in the book) Consider the line of code: if( !feof(wq) ) fread(oops, sizeof(float), 3, wq); 7a. What is the name of the file pointer? ANSWER: wq Read from FILE pointer fp, start at address oops, read in checks of sizeof(float), 3 times 7b. Because there are 4 bytes in a floating point number, how many bytes would be read when 'fread' is successfully executed? ANSWER: 4*3=12 bytes 8. (14 i the book) Why is it important to use feof with binary files? ANSWER: Binary data may contain and EOF.