MATLAB中~feof和feof有什么区别? 10

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=fop... 展开
推荐于2017-05-20 13:27:20 最佳答案
数feof(fid) 用法,所以只循环一次就终止了,你仔细想下就会明白的,是逻辑表达式;否则返回“假”,运行结果自然为0

这些都是程序设计的基本知识:文件指针到达文件末尾时 该表达式值为“假”;

while ~feof 表示 若 未读到文件末尾 则 继续 循环
while feof 表示 若 未读到文件末尾 则 终止 循环;否则为“真”;

~feof 是在 feof 前加了“非”:文件指针 fid 到达文件末尾时返回“真”值


(See fopen for a complete description of fid.
[tline,msg] = fgets(obj)

tline = fgets(obj) reads one line of text from the device connected to the serial port object,count. To exclude the terminator.

fgets (serial)
Read line of text from device and include terminator
tline = fgets(obj)
The end-of-file indicator is set when there is no more input from the file,msg] = fgets(obj) returns a warning message to msg if the read operation was unsuccessful,count,count] = fgets(obj)
eofstat = feof(fid) returns 1 if the end-of-file indicator for the file fid has been set and 0 otherwise, use fgetl. This returned data includes the terminator with the text line, including the terminator, obj, and returns the data to tline,count] = fgets(obj) returns the number of values read to count
QIANGONG120 | 发布于2015-10-19 13:10