differnce between fgetl and fgets

19 visualizaciones (últimos 30 días)
Tor Fredrik Hove
Tor Fredrik Hove el 27 de Oct. de 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

Respuesta aceptada

Jan
Jan el 27 de Oct. de 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.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by