Bird
0
0

What is wrong with this MATLAB code?

medium📝 Debug Q7 of 15
MATLAB - File I/O
What is wrong with this MATLAB code?
fid = fopen('data.txt', 'w');
fprintf(fid, 'Number: %f\n', 3.14);
Afprintf format specifier %f is invalid
BFile is not closed after writing
Cfopen mode 'w' is incorrect
Dfprintf cannot write floats
Step-by-Step Solution
Solution:
  1. Step 1: Check file opening and writing

    File opened in write mode and fprintf writes float correctly.
  2. Step 2: Check file closing

    File is not closed with fclose, which can cause data loss or locking.
  3. Final Answer:

    File is not closed after writing -> Option B
  4. Quick Check:

    Always fclose files after writing [OK]
Quick Trick: Always fclose after fopen to save data [OK]
Common Mistakes:
  • Forgetting fclose
  • Thinking %f is invalid
  • Using wrong fopen mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes