Bird
0
0

Identify the error in this MATLAB code snippet:

medium📝 Debug Q6 of 15
MATLAB - File I/O
Identify the error in this MATLAB code snippet:
fid = fopen('log.txt', 'r');
fprintf(fid, 'Test\n');
fclose(fid);
AFile opened in read mode but used for writing
Bfprintf syntax is incorrect
Cfclose missing argument
DNo error
Step-by-Step Solution
Solution:
  1. Step 1: Check fopen mode

    File opened with 'r' (read) mode, so writing is not allowed.
  2. Step 2: Check fprintf usage

    fprintf tries to write to file, causing error because file is not writable.
  3. Final Answer:

    File opened in read mode but used for writing -> Option A
  4. Quick Check:

    File mode must match operation [OK]
Quick Trick: Open files with 'w' to write, 'r' to read [OK]
Common Mistakes:
  • Using 'r' mode for writing
  • Incorrect fprintf syntax
  • Forgetting fclose argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes