help with file handing and if-elseif-else

5 vues (au cours des 30 derniers jours)
Edgar Gonzalez
Edgar Gonzalez le 28 Sep 2018
Modifié(e) : Stephen23 le 28 Sep 2018
The 'else' portion of this code will not work. If I type in something not found in the file nothing will be returned but 'City not found' is supposed to be returned. Can anyone help?
This is the code:
fid=fopen('cities.txt','r'); % opens the file
city = input('Enter a city ','s'); % asks for input and converts it to string
while ~feof(fid) % tests for end of file
l=fgets(fid);
if ~contains(l,city), continue, end % keep going until found
a = str2double(l(1:2)); % defines variables for coordinates
b = str2double(l(4:5));
c = str2double(l(8:9));
d = str2double(l(11:12));
if b > 66 % if the coordinate in range print
fprintf(city)
fprintf(' is in a polar region \n')
elseif b > 35 && b < 66
fprintf(city)
fprintf(' is in a temperate region \n')
elseif b < 35
fprintf(city)
fprintf(' is in a tropical region \n')
else
fprintf('City not found!')
end
end
fclose(fid); % closes file

Réponses (1)

Walter Roberson
Walter Roberson le 28 Sep 2018
Modifié(e) : Walter Roberson le 28 Sep 2018
You have
while ~feof(fid) % tests for end of file
l=fgets(fid);
if ~contains(l,city), continue, end % keep going until found
Suppose you have not reached end of file, then you fgets() to read a line. Then you check to see if the line contains the target string. If it does not, you "continue", which means you loop back to the "while" . Still not end of file? Read a line, test, it still doesn't contain, loop back. Under the assumption that the target string is not in the file, then contains() is never true, so you just keep looping until the feof() becomes true and ~feof() becomes false and you exit the loop -- without ever once having tested the value of b .
You should probably be using something like
found = false;
while ~found & ~feof(fid)
...
fprintf(' is in a polar region \n');
found = true;
break;
...
end
if ~found
fprintf('City not found!');
end
By the way: consider using the 'IgnoreCase' option of contains(). But if you do, watch out for ẞ because the rules for ignoring case might possibly not be well defined.
  2 commentaires
Edgar Gonzalez
Edgar Gonzalez le 28 Sep 2018
Modifié(e) : Stephen23 le 28 Sep 2018
using
while ~found & ~feof(fid)
returned this error
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in test (line 7)
while ~found & ~feof(fid) % tests for end of file
but the file has already been opened in the first line
Walter Roberson
Walter Roberson le 28 Sep 2018
filename = 'cities.txt';
[fid, msg] = fopen(filename, 'r');
if fid < 0
error('Could not open file "%s" because "%s"', filename, msg);
end
city = input('Enter a city ','s'); % asks for input and converts it to string
found = false;
while ~found && ~feof(fid) % tests for end of file
l=fgets(fid);
if ~contains(l,city), continue, end % keep going until found
a = str2double(l(1:2)); % defines variables for coordinates
b = str2double(l(4:5));
c = str2double(l(8:9));
d = str2double(l(11:12));
if b > 66 % if the coordinate in range print
fprintf(city);
fprintf(' is in a polar region \n');
found = true;
break;
elseif b > 35 && b < 66
fprintf(city);
fprintf(' is in a temperate region \n');
found = true;
break;
elseif b < 35
fprintf(city);
fprintf(' is in a tropical region \n');
found = true;
break;
else
fprintf('city found, but b was infinite or nan or exactly equal to 66 or exactly equal to 35!')
found = true;
break;
end
end
fclose(fid); % closes file
if ~found
fprintf('City not found!');
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by