differnce between fgetl and fgets
조회 수: 16 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Jan
2011년 10월 27일
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.
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!