fid=fopen('magic.m','r');
count=0;
while ~feof(fid)
line=fgetl(fid);
if isempty(line)|strncmp(line,'%',1)
continue
end
count=count+1;
end
disp(sprintf('%d lines',count)); 运行结果是25lines
fid=fopen('magic.m','r');
count=0;
while feof(fid)
line=fgetl(fid);
if isempty(line)|strncmp(line,'%',1)
continue
end
count=count+1;
end
disp(sprintf('%d lines',count)); 运行结果为0 lines
这是为什么啊 ?请高人解答 谢谢!!
函数feof(fid) 用法:文件指针 fid 到达文件末尾时返回“真”值;否则返回“假”;
~feof 是在 feof 前加了“非”,是逻辑表达式:文件指针到达文件末尾时 该表达式值为“假”;否则为“真”;
while ~feof 表示 若 未读到文件末尾 则 继续 循环
while feof 表示 若 未读到文件末尾 则 终止 循环,所以只循环一次就终止了,运行结果自然为0
这些都是程序设计的基本知识,你仔细想下就会明白的。
~feof 是在 feof 前加了“非”,是逻辑表达式:文件指针到达文件末尾时 该表达式值为“假”;否则为“真”;
while ~feof 表示 若 未读到文件末尾 则 继续 循环
while feof 表示 若 未读到文件末尾 则 终止 循环,所以只循环一次就终止了,运行结果自然为0
这些都是程序设计的基本知识,你仔细想下就会明白的。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2015-10-19
Description
eofstat = feof(fid) returns 1 if the end-of-file indicator for the file fid has been set and 0 otherwise. (See fopen for a complete description of fid.)
The end-of-file indicator is set when there is no more input from the file.
fgets (serial)
Read line of text from device and include terminator
Syntax
tline = fgets(obj)
[tline,count] = fgets(obj)
[tline,count,msg] = fgets(obj)
Description
tline = fgets(obj) reads one line of text from the device connected to the serial port object, obj, and returns the data to tline. This returned data includes the terminator with the text line. To exclude the terminator, use fgetl.
[tline,count] = fgets(obj) returns the number of values read to count, including the terminator.
[tline,count,msg] = fgets(obj) returns a warning message to msg if the read operation was unsuccessful.
eofstat = feof(fid) returns 1 if the end-of-file indicator for the file fid has been set and 0 otherwise. (See fopen for a complete description of fid.)
The end-of-file indicator is set when there is no more input from the file.
fgets (serial)
Read line of text from device and include terminator
Syntax
tline = fgets(obj)
[tline,count] = fgets(obj)
[tline,count,msg] = fgets(obj)
Description
tline = fgets(obj) reads one line of text from the device connected to the serial port object, obj, and returns the data to tline. This returned data includes the terminator with the text line. To exclude the terminator, use fgetl.
[tline,count] = fgets(obj) returns the number of values read to count, including the terminator.
[tline,count,msg] = fgets(obj) returns a warning message to msg if the read operation was unsuccessful.