Arrays

3 次查看(过去 30 天)
Sean
Sean 2011-11-10
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);

回答(1 个)

Walter Roberson
Walter Roberson 2011-11-10
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.

类别

Help CenterFile 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!

Translated by