Azzera filtri
Azzera filtri

differnce between fgetl and fgets

11 visualizzazioni (ultimi 30 giorni)
Tor Fredrik Hove
Tor Fredrik Hove il 27 Ott 2011
definition for fgets
tline = fgets(fileID) reads the next line of the specified file, including the newline characters.
definition for fgetl
tline = fgetl(fileID) returns the next line of the specified file, removing the newline characters.
I tried both on a file with \n but got the same resultat
>> fid=fopen('sweden.se', 'r')
fid =
5
>> while ~feof(fid) fgetl(fid) end
ans =
var sommer
ans =
host vinter2 3
ans =
5 6
ans =
4 7
>> fclose(fid)
ans =
0
>> fid=fopen('sweden.se', 'r')
fid =
5
>> while ~feof(fid) fgets(fid) end
ans =
var sommer
ans =
host vinter2 3
ans =
5 6
ans =
4 7
>>
the file sweden.se is exactly as both read it:
var sommer
host vinter2 3
5 6
4 7

Risposta accettata

Jan
Jan il 27 Ott 2011
The results are very similiar. Using your method the difference is hard to see, because the line break appears at the end of the line as line break directly followed by a line break.
Better check this by a numerical display of the ASCII values:
fid = fopen('sweden.se', 'r')
while ~feof(fid)
s = fgetl(fid);
% disp(s);
disp(double(s));
end
fclose(fid);
fid = fopen('sweden.se', 'r')
while ~feof(fid)
s = fgets(fid);
% disp(s);
disp(double(s));
end
fclsoe(fid);
You can read the source of the file fgetl.m to see, what happens.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by