Files: For Multiple-Choice and Essay Questions
Files: For Multiple-Choice and Essay Questions
Files
1
A. Multiple-choice questions and essay questions (10pts)
A.1. Which of the following statements about fscanf and scanf is TRUE?
fscanf read from standard input while scanf specifies a stream from which to read
fscanf specifies a stream from which to read while scanf read only from standard input
fscanf and scanf has no difference in their functions
fscanf and scanf can read from specified stream
Selina
A.2. Consider the text file, “abc.txt”, that contains a list of names, as shown
Hebe
aside. Each name follows immediately with a new line character. Ella
What is the output of the following code segment? Justify your answer. Jack
Assume that all required headers are included. Rose
A.4. What is the output of the following code segment? Justify your answer. Assume that all
required headers are included.
2
FILE* fp = fopen("abc.txt", "wt");
fputs(fp, "This is an apple");
long pos = ftell(fp);
fseek(fp, 9, SEEK_SET);
fputs(fp, " sam");
fclose(fp);
A.5. What is the output of the following code segment? Justify your answer. Assume that all
required headers are included.
/* Create a file containing two integers */
FILE *out_fp = fopen("abc.txt", "wt");
fputs("123 456", out_fp);
fclose(out_fp);
/* Attempt to read a character, then an integer, then a character
again, then an integer again, from the file just created. */
char character;
int integer;
FILE* in_fp = fopen("abc.txt", "rt");
fscanf(in_fp, "%c %d ", &character, &integer);
cout << "character: '" << character << "'\n";
cout << "integer: " << integer << "\n";
fscanf(in_fp, "%c %d ", &character, &integer);
cout << "character: '" << character << "'\n";
cout << "integer: " << integer << "\n";
fclose(in_fp);
A.7. Why should a program close a file when it has finished using it?
A.8. What are the differences between the two classes, FILE and fstream?
A.9. What are the C functions used to read or write a file in in Binary Mode? Give an example to
demonstrate those functions.
A.10. Differentiate the following pairs of functions, puts vs. fputs and gets vs. fgets.
3
B. Programming questions (10pts)
The input values are guaranteed to be valid. Note that you need to create a solution with a single
main function. The code for each question is organized in a separate function, and these functions
must be called from the main function with appropriate arguments.
Expected output:
1 2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97
2
100
Expected output:
10 9 8 7 6 5 4 3 2 1 23
B.3. The file grades.txt includes lines of text, each of which contains the student’s ID, name (a
single word) and score, as demonstrated below.
1 student1 3.5
2 student2 0.6
3 student3 4.0
4 student4 2.2
. . .
Write a function to input a student ID, search in given file the student with the specified
student ID, and then print out his/her name and GPA.
4
B.4. Consider two text files, one of them contains a sequence of 10 odd positive integers, and the
other file contains a sequence of 10 even positive integers. In both files, two adjacent
numbers are separated by a single space. Write a function to merge the numbers in these
two files such that they come in an alternative fashion and then write the resulting content
to a new text file
Test data:
FileA FileB
9 5 7 3 1 11 15 13 17 19 8 4 6 12 2 10 14 18 16 20
9 8 5 4 7 6 3 12 1 2 11 10 15 14 13 18 17 16 19 20
B.5. This question is the reverse version of the above question. Consider a text file that contains a
sequence of 20 positive integers such that the odd and even numbers come in an alternate
fashion. Write a function to split the numbers to two text files such that the first file contains
odd numbers only and the second file contains even numbers only. The numbers must
appear in the same order as they do in the original file.
Test data:
9 8 5 4 7 6 3 12 1 2 11 10 15 14 13 18 17 16 19 20
FileA FileB
9 5 7 3 1 11 15 13 17 19 8 4 6 12 2 10 14 18 16 20
B.6. Assume that the file abc.txt includes several lines of text. Write a function to receive a non-
negative integer, which is smaller than the number of lines, insert your name to the line
whose index (starts from 0) is the specified integer, and write the resulting content to a new
text file.
Test data:
Input a non-negative integer: 2
abc.txt
Dear March - Come in -
How glad I am -
I hoped for you before
B.7. Consider a text file that contains a single string, “happy new year”. Write a function that
uses ftell and fseek to modify the string such that the new content is “happy
birthday”. Note that you are not allowed to overwrite the whole string.
B.8. Consider the following two text files, FileA and FileB.
i never saw a purple cow the forest is the town of trees
i never hope to see one where they live quite at their ease
but i can tell you, anyhow with their neighbors at their side
i would rather see than be one just as we in cities wide
Write a function to merge these two files into a single file such that their lines come in an
alternative fashion, that is, the first line of FileA goes first and it is followed by the first line
of FileB, and then the second line of FileA and FileB, respectively, and so on.
B.9. Consider a text file that contains letters only. Write a function to flip the letter case of every
letter read from the given file and write the resulting content to a new text file.
B.10. Consider a text file that contains letters only. Write a function to encrypt the given file,
using your own algorithm, and store the encrypted content to the file MyEncryption.enc.
After that, decrypt the previously encrypted file and store the decrypted content to the file
MyDecryption.dec.
Expected output: