私は、ファイルを非常に単純に読み取り、ファイルに含まれていたものを適切に出力するコードを書いています。
私はいつもそのようなプログラムをファイルの終わりで終了させるのに苦労していて、適切な解決策を見つけたと思いますが、私を超えた理由で、各行が私の出力で2回印刷されています。
これが私のメインファイルです:
int main(int argc, char *argv[]) {
// insure 2 arguments given, one for a.out and one for the test file
if (argc != 2) {
// result if request fails
printf("Requires 2 arguments. Be sure to include test file location\n");
return 0;
}
FILE *fp; //open the file
fp = fopen(argv[1], "r");
char option;
int key;
int i = 0;
while (fscanf(fp, "%c %d", &option, &key) != EOF) {
printf("%d\n", key);
}
}
重要なのは2回印刷することです!
うまくいけば、これは問題への過度の露出のために私が見落としている単純なエラーです。