Arrays

2 views (last 30 days)
Sean
Sean on 10 Nov 2011
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);

Answers (1)

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

Tags

Community Treasure Hunt

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

Start Hunting!