0

これは、大規模なプログラムで使用する前に、fopen および同様の構文のコツをつかむために主に作成されたプログラムからのものです。したがって、プログラムが実行しようとしている唯一のことは、ファイル (scores.dat) を開き、そのファイルのデータを読み取り、それを配列に割り当て、配列を出力することです。

これは、エラーが発生したコードのセグメントです。

int scores[13][4];

FILE *score; 
score = fopen("scores.dat", "r");

fscanf("%d %d %d %d", &scores[0][0], &scores[0][1], &scores[0][2], &scores[0][3]);

printf("%d &d %d %d", scores[0][0], scores[0][1], scores[0][2], scores[0][3]);

fclose(score);

コンパイルすると、次のエラーが表示されます。

text.c: In function 'main':
text.c:15: warning: passing argument 1 of 'fscanf' from incompatible pointer type
text.c:15: warning: passing argument 2 of 'fscanf' from incompatible pointer type

どうすれば修正できますか?

重要な場合、score.dat は次のようになります。

88 77 85 91 65 72 84 96 50 76 67 89 70 80 90 99 42 65 66 72 80 82 85 83 90 89 93 
98 86 76 85 99 99 99 99 99 84 72 60 66 50 31 20 10 90 95 91 10 99 91 85 80
4

3 に答える 3

5

の最初の引数がありませんfscanf():

fscanf(score, "%d %d %d %d", &scores[0][0], ... etc.
       ^^^^^
       this needs to be a `FILE *`, and not `const char *`.
于 2013-04-22T21:54:59.147 に答える
4

ファイルに言及するのを忘れました:

fscanf(score, "%d %d %d %d", &scores[0][0], ...);
//     ^^^^^
于 2013-04-22T21:54:54.323 に答える
1

を正しく使用しているため、理解fopen()は問題ありませんが、渡した引数がfscanf()そのプロトタイプと一致しません。そのプロトタイプは次のとおりです。

int fscanf ( FILE *, const char * , ... );

したがって、次を使用する必要があります。

fscanf(source,"%d %d %d %d", &scores[0][0], &scores[0][1], &scores[0][2], &scores[0][3]);

を使用してファイルを開くときにエラーが発生したときにメッセージを表示し、プログラムを終了するfopen()コードを含めることをお勧めします。fopen()何かのようなもの:

if(source==NULL)
{
 printf("Error opening file");
 exit(1);
}
于 2013-04-22T22:10:09.267 に答える