필터 지우기
필터 지우기

differnce between fgetl and fgets

조회 수: 11 (최근 30일)
Tor Fredrik Hove
Tor Fredrik Hove 2011년 10월 27일
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

채택된 답변

Jan
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개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by