Arrays

조회 수: 2 (최근 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