フィルターのクリア

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 Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by