0

次のようなコードの一部を使用します。

fscanf(f1, "%d", &n);
while(!feof(f1)){
...
fscanf(f1, "%d", &n);}

ファイルの最後の行がありません (EOF に達しています)。どのように解決すればよいですか?

私が見つけた唯一の解決策は次のとおりです。

if (fscanf(f1, "%d", &n)!=EOF){
rewind(f1);
do{
...
fscanf(f1, "%d", &n);
}while(!feof(f1));
}
4

1 に答える 1

3

fscanf をループの最後に置きます。fscanf は、番号が確実に終了するまで読み取ります。入力ファイルの最後の文字が (スペースや改行ではなく) 数字である場合、最後の行を解析するとき (改行で終わらない行を「不完全な最後の行」と呼ぶ人もいます)、 fscanf は EOF にヒットし、番号の最後を見つけると、EOF がヒットしたため、feof が真になります。

feof をチェックするのではなく、fscanf のリターン コードをチェックする必要があります。数値として解析できる数字があるかどうかがわかります。

ファイルに含まれていると仮定します"11\n23"

f = fopen(...);
result = fscanf(f, "%d", &i);
// result == 1, because one variable has been read
// i == 11, because that's the first number
// file pointer is past the '\n', because '\n'
//   had to be read to find out the number is not
//   something like 110
//   The '\n' itself has been put back using ungetc
// feof(f) == 0, because nothing tried to read past EOF

result = fscanf(f, "%d", &i);
// result == 1, because one variable has been read by this call
// i == 23 (obviously)
// file pointer is at EOF (it can't go further)
// feof(f) == 1, because fscanf tried to read past
//   the '3' to check whether there were extra
//   characters.

//  (Your loop terminates here, because feof(f) is true

result = fscanf(f, "%d", &i);
// result == EOF (error before first variable)
// i is likely unchanged. I am unsure whether this
//   is guaranteed by the language definition
// file pointer unchanged
// feof(f) still true

// You should terminate processing *NOW*, because
// return is no longer one.
于 2015-07-26T09:23:26.887 に答える