Text file i/o problem....I always get 0 with charnum ...whats the problem with the code??
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marco Nashaat
am 9 Feb. 2020
Kommentiert: Walter Roberson
am 9 Feb. 2020
function charnum=char_counter(fname,character)
fid=fopen(fname,'rt')
if fid<0
charnum=-1;
return;
end
if fid ~=0 && ischar(character)==true
charnum=count(fname,"character ");
else
charnum=-1;
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 9 Feb. 2020
Bearbeitet: Stephen23
am 9 Feb. 2020
Your code has likely has several bugs, e.g. you were checking for literal "character" as opposed to using the contents of the character variable:
charnum = count(fname,character);
One more bug is that you are checking the input fname for these characters... but I suspect that you need to import the file contents and check that, but nowhere in your code do you actually import the file contents.
3 Kommentare
Stephen23
am 9 Feb. 2020
For a valid file fid will be >=3, not >0, so you might need something like this:
fid = fopen(fname,'rt')
if fid>=3
... import file contents, etc.
charnum = ...
else
charnum = -1;
end
Walter Roberson
am 9 Feb. 2020
Testing 0 is fine.
You are counting characters in the file name instead of reading from the file and counting the characters in what results.
By the way, see fileread()
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Database Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!