Question:

Which method moves a file pointer to the nth character with respect to r position?

Updated On: Mar 29, 2025
  • fp.seek(r)
  • fp.seek(n)
  • fp.seek(n, r)
  • seek(n, r).fp
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

(1) fp.seek(r): This is incorrect because it only specifies the relative position 'r' but not the offset 'n'. 'seek' requires both an offset and a position to move the file pointer.

(2) fp.seek(n): This is also incorrect. While it specifies the offset 'n', it does not specify the reference position from which to seek. The 'seek' function needs a reference point.

(3) fp.seek(n, r): This is the correct syntax. It takes the file pointer to the 'n'th character with respect to the 'r' position. 'n' is the offset, and 'r' is the reference position (0: beginning, 1: current, 2: end).

(4) seek(n,r).fp: This is incorrect because the file pointer should come first when calling the function.

Therefore, the correct method is (3) fp.seek(n, r).

Was this answer helpful?
0
0