필터 지우기
필터 지우기

Script to read, error check till user enters a valid file name that can be opened, then print out file name and close file.

조회 수: 2 (최근 30일)
I am having the hardest trouble on the loop error-checking part that keeps checking still the user enters a valid filename that can be opened.
This is what I have so far:
% prompt user for name to be read and open
file = input('PLEASE INPUT FILE NAME TO BE READ:','s');
fopen(file,'r');
% loop error-check until valid filename can be opened
% print file name and close file
while fopen(file)~=0
fpirnt('FILE OPENED IS: %s\n',file);
fclose(file);
else
fprintf('PLEASE INPUT A VALID FILE NAME!\n','s');
file = input('PLEASE INPUT FILE NAME TO BE READ:','s');
end
what is going wrong?
Thank you!

채택된 답변

Walter Roberson
Walter Roberson 2013년 10월 25일
'while' does not permit an 'else', so you have a syntax error.
You cannot close a file by name, only by file identifier.
Hint: "break"
  댓글 수: 1
Nora
Nora 2013년 10월 25일
% prompt user for name to be read and open file = input('PLEASE INPUT FILE NAME TO BE READ:','s'); fopen(file,'r'); % loop error-check until valid filename can be opened % print file name and close file if fopen(file)~=0 fpirnt('FILE OPENED IS: %s\n',file); fclose(file); else fprintf('PLEASE INPUT A VALID FILE NAME!\n','s'); file = input('PLEASE INPUT FILE NAME TO BE READ:','s'); end
I still get the problem if I use and if-loop. But I don't know how to do the other parts:

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by