0
那麼我在做什麼基本上我正在讀取文件,將其保存到數組,並嘗試打印它。但是,我的代碼似乎有什麼問題?文件處理代碼編譯但不運行
#include<stdio.h>
#include<string.h>
typedef struct{
int studentNumber; //struct declaration
char name[31];
float quizzes[8];
float exams[3];
float overallGrade;
float standing;
}Student;
//function for transferring
void transfer(Student myList[]){
int x=0;
for (x=0; x<3;x++){
printf("%d\n",myList[x].studentNumber);
printf("%d\n",myList[x].quizzes);
printf("%d\n",myList[x].exams);
}
}
int main(){
FILE *read = NULL; //creates a stream
Student myList[50];
int ctr=0;
char buf[128];
read = fopen("file.txt", "r"); //opens the file
if(read==NULL)
printf("FILE NOT FOUND!");
else{
while(!feof(read)){
fgets(buf, sizeof(buf), read); //reads first line
sscanf(buf, "%d,", myList[0].studentNumber);
fgets(buf, sizeof(buf), read); //reads second Line
sscanf(buf, "%d,%d,%d,%d,%d,%d,%d,%d", &myList[ctr].quizzes[0],&myList[ctr].quizzes[1],&myList[ctr].quizzes[2],&myList[ctr].quizzes[3],&myList[ctr].quizzes[4],&myList[ctr].quizzes[5],&myList[ctr].quizzes[6],&myList[ctr].quizzes[7]);
fgets(buf, sizeof(buf), read); //reads third Line
sscanf(buf, "%d,%d,%d", &myList[ctr].exams[0], &myList[ctr].exams[1],&myList[ctr].exams[2]);
ctr++;
}
}
fclose(read);
transfer(myList);
return 0;
}
該文件由三條主線構成。首先是學生數量,第二是測驗,第三行是考試。該文件看起來像這樣:
1234567
12,16,14,9,8,15,0,3
40,40,40
1237890
13,12,18,11,7,15,10,8
40,35,50
1232098
10,11,15,10,0,15,6,7
36,42,40
您能否澄清您遇到的問題(超出「不運行」的範圍)以及您嘗試解決的問題? –
基本上就是這樣。正如你所看到的,代碼編譯,但是當我運行它時,它突然崩潰。我正在做的是通過fgets函數逐行讀取文件,並使用sscanf從一個struct數組中保存整數。 –
什麼是崩潰?它是否是seg斷層? – drum