fseek() does not accept negative offsets from the start (SEEK_SET). Negative values cause an error.
Step 2: Validate other parts
fopen('data.txt', 'r') is correct for reading. ftell() works after fseek(). Pointer is at start by default, not end.
Final Answer:
Negative offset in fseek causes error -> Option A
Quick Check:
fseek negative offset invalid [OK]
Hint: fseek offset must be zero or positive [OK]
Common Mistakes:
Using negative offset with SEEK_SET
Confusing fopen modes
Thinking ftell fails after fseek
5. You want to update the 10th byte in a file without reading the whole file. Which sequence of PHP functions correctly moves the pointer and writes a character 'X' at that position?
hard
A. fopen($file, 'w'); fseek($file, 10); fwrite($file, 'X');
B. fopen($file, 'r'); rewind($file); fwrite($file, 'X');
C. fopen($file, 'r+'); fseek($file, 9); fwrite($file, 'X');
D. fopen($file, 'a'); fseek($file, 10); fwrite($file, 'X');
Solution
Step 1: Choose correct mode to read and write
Mode 'r+' opens file for reading and writing without truncating it, allowing pointer movement and writing.
Step 2: Move pointer and write
fseek($file, 9); moves pointer to 10th byte (index 9). fwrite($file, 'X'); writes 'X' at that position.
Step 3: Check other options
Mode 'w' truncates file, 'r' alone can't write, 'a' appends at end ignoring fseek.
Final Answer:
fopen($file, 'r+'); fseek($file, 9); fwrite($file, 'X'); -> Option C
Quick Check:
Use 'r+' mode and fseek before fwrite [OK]
Hint: Use 'r+' mode and fseek to update file byte [OK]
Common Mistakes:
Using 'w' mode which erases file
Using 'a' mode which always appends
Not moving pointer before writing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.