In: Computer Science
Which of the following statement will, move the file pointer to the third ‘r’ in the file show below. Assume the newline character is 1 byte. Select all that apply.
With
great
power
comes
great
responsibility
Answers:
Spidey.seekg(-19L, ios:end);
Spidey.seekg(‘r’, ios:beg);
Spidey.seekg(24, ios:cur);
Spidey.seekg(“r?r?r”, ios:beg);
Spidey.seekg(-19L, ios:end);
This statement will move the pointer to third 'r' in the file. -19 means go back from the ios::end. The length of the "responsibility" is 14. One byte for new line character and 3 for "eat" from second last great. total becomes 18. To move pointer to 'r', add one more. The total becomes 19 positions.
Spidey.seekg(‘r’, ios:beg);
Valid statement, but there is no overloaded seekg which takes character as input. What compiler does, it converts the 'r' to its equivalent ASCII value and move the pointer to that position from beginning.
Spidey.seekg(24, ios:cur);
Valid statement.
Using above character count for each line, to move to third 'r', need to move pointer by:
5+ 6 + 6 + 6 + 1 (only g from great) = 24
Spidey.seekg(“r?r?r”, ios:beg);
Not a valid statement. C++ provided no overloaded function which takes string as first argument in seekg.