You have made a common mistake in thinking that feof() predicts EOF, and that consequently if feof() is false that there is something to read. That is not how it works in MATLAB or C: instead feof() is only true until a read is attempted that fails due to there being no more data. Thus, when you actually do the read, you have to check then whether you actually got any data from the attempt.
Arrays
3 次查看(过去 30 天)
显示 更早的评论
Does my code look correct. I am trying to display the name along with the average number of hours worked. I am reading them in through a text file.
index=0;
total=0;
inFile=fopen('weeklyHours.txt','r');
outFile=fopen('employeeHours.txt','w');
while~feof(inFile);
line=fgets(inFile);
total=average+(inFile);
index=index+1;
for i=1:1:8
average=total/7;
total=0;
end
end
for i=1:1:1;
name=employee{j};
end
fprintf('%s average hours worked: %1.2f\n',name,average);
fclose(inFile);
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!