Q:

Write a script that will prompt the user for the name of a file from which to read. Loop to error-check until the user enters a valid filename that can be opened

0

Write a script that will prompt the user for the name of a file from which to read. Loop to error-check until the user enters a valid filename that can be opened. (Note: this would be part of a longer program that would actually do something with the file, but for this problem all you have to do is to error-check until the user enters a valid filename that can be read from.) 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch9Ex9.m

% Prompt the user for a file name and error-check

% until the user enters a valid file name

fname = input('Enter a file name: ','s');

fid = fopen(fname);

while fid == -1

 fname = input('Error! Enter a valid file name: ', 's');

end

%Error-check file close

closeresult = fclose(fid);

if closeresult == 0

 disp('File close successful')

else

 disp('File close not successful')

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now