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 Center および File ExchangeLow-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