Bird
0
0

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📝 Application Q15 of 15
PHP - File Handling
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?
Afopen($file, 'w'); fseek($file, 10); fwrite($file, 'X');
Bfopen($file, 'r'); rewind($file); fwrite($file, 'X');
Cfopen($file, 'r+'); fseek($file, 9); fwrite($file, 'X');
Dfopen($file, 'a'); fseek($file, 10); fwrite($file, 'X');
Step-by-Step Solution
Solution:
  1. 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.
  2. Step 2: Move pointer and write

    fseek($file, 9); moves pointer to 10th byte (index 9). fwrite($file, 'X'); writes 'X' at that position.
  3. Step 3: Check other options

    Mode 'w' truncates file, 'r' alone can't write, 'a' appends at end ignoring fseek.
  4. Final Answer:

    fopen($file, 'r+'); fseek($file, 9); fwrite($file, 'X'); -> Option C
  5. Quick Check:

    Use 'r+' mode and fseek before fwrite [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes