アプリ版:「スタンプのみでお礼する」機能のリリースについて

Visial C++ 2008を使用したプログラミングについてです。
学校の課題をやるためやむなくVisial C++2008を使用していて
学校では実行が出来たのに家では出来ませんでした。
ソースファイルは以下のとおりです。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*構造体宣言*/
struct Students{
int N;
int A;
int B;
int C;
};

int main(void){
struct Students std[50]; /*構造体型配列*/
int i,N,A,B,C,num,scannum;

srand((unsigned)time(NULL)); /*乱数の初期化*/

FILE *file; /*ファイルのポインタを用意*/

file=fopen("Data.txt","r"); /*Dataファイルの読み込み*/

/*ファイルのオープンチェック*/
if(file==NULL){
fprintf(stderr,"cannnot open file 'Data.txt'\n");
exit(1);
}
/*Studentsにデータを格納*/
for(i=0;i<=49;i++){
fscanf(file,"%d%d%d",&N,&A,&B);
std[i].N=N;
std[i].A=A;
std[i].B=B;
std[i].C=70+(rand()/(RAND_MAX+1.0)*31);

/*表示*/
printf("学籍番号:%d.",std[i].N);
printf("科目A:%d.\n",std[i].A);
printf("科目B:%d.\n",std[i].B);
printf("科目C:%d.\n",std[i].C);
}

fclose(file);

return 0;
}

エラーとしては
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2275: 'FILE' : この型は演算子として使用できません
1> c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(69) : 'FILE' の宣言を確認してください。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2065: 'file' : 定義されていない識別子です。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : error C2065: 'file' : 定義されていない識別子です。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : warning C4047: '=' : 間接参照のレベルが 'int' と 'FILE *' で異なっています。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : error C2065: 'file' : 定義されていない識別子です。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : warning C4047: '==' : 間接参照のレベルが 'int' と 'void *' で異なっています。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : error C2065: 'file' : 定義されていない識別子です。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4024: 'fscanf' : の型が 1 の仮引数および実引数と異なります。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(36) : warning C4244: '=' : 'double' から 'int' への変換です。データが失われる可能性があります。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : error C2065: 'file' : 定義されていない識別子です。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。
1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4024: 'fclose' : の型が 1 の仮引数および実引数と異なります。
1>ビルドログは "file://c:\Documents and Settings\devil\My Documents\Visual Studio 2008\Projects\テスト\テスト\Debug\BuildLog.htm" に保存されました。

と表示されます。FILEの宣言をしているのになんで確認してください
と出るのでしょうか。。。。解決にご協力お願いします。m(__)m

A 回答 (2件)

 こんばんは。



 VC++6.0の*.Cファイルにて確認してみました。
 一応コンパイルは通りました。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*構造体宣言*/
struct Students{
int N;
int A;
int B;
int C;
};

int main(void){
/*stdという名前が衝突しているので名前変更*/
struct Students arr[50]; /*構造体型配列*/
int i,N,A,B,C,num,scannum;
/*fileの宣言が処理の下にかかれていたので上に持ってきた*/
FILE *file; /*ファイルのポインタを用意*/

srand((unsigned)time(NULL)); /*乱数の初期化*/

file=fopen("Data.txt","r"); /*Dataファイルの読み込み*/

/*ファイルのオープンチェック*/
if(file==NULL){
fprintf(stderr,"cannnot open file 'Data.txt'\n");
exit(1);
}
/*Studentsにデータを格納*/
for(i=0;i<=49;i++){
fscanf(file,"%d%d%d",&N,&A,&B);
arr[i].N=N;
arr[i].A=A;
arr[i].B=B;
arr[i].C=70+(rand()/(RAND_MAX+1.0)*31);

/*表示*/
printf("学籍番号:%d.",arr[i].N);
printf("科目A:%d.\n",arr[i].A);
printf("科目B:%d.\n",arr[i].B);
printf("科目C:%d.\n",arr[i].C);
}

fclose(file);

return 0;
}
    • good
    • 0
この回答へのお礼

詳細をありがとうございます。php504さんのアドバイスのとおり
宣言と乱数の初期化が反対になっていたのが原因だったんですね。。
学校のPCではうまく実行できたのにVisial C++で出来なかったので
別の問題かと思ってしまいました。親切にありがとうございました。
m(__)m

お礼日時:2008/07/11 23:34

Cでは変数の宣言を最初にしないといけません


srand((unsigned)time(NULL)); /*乱数の初期化*/
FILE *file; /*ファイルのポインタを用意*/
を入れ替えて
FILE *file; /*ファイルのポインタを用意*/
srand((unsigned)time(NULL)); /*乱数の初期化*/
にしてみてください
    • good
    • 0
この回答へのお礼

回答ありがとうございます。入れ替えた結果望ましい実行結果が
出ました。このミスには気をつけないといけませんね^^;
ご教授ありがとうございました。m(__)m

お礼日時:2008/07/11 23:38

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!