即時試圖從一個文件的文件的設置是這樣讀出的數據:將數據讀入2個陣列
3050 76
2030 60
1600 70
2222 50
2430 60
2800 50
0 0
第一個數字代表一個學生ID,第二個數字代表學生的 級時程序達到0 0時,應該停止從文件讀取
這裏是一個程序的例子,它沒有從文件中讀取,因爲出現一個稱爲SEGMENTATION FAULT的錯誤。我正在使用Ubuntu的,這是發生此錯誤
// this is the grading sorting program for part one
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
int main(int argc, char **argv)
{
FILE* fp; // file pointer, points to the file
char file_name[32]; // store file name
int ID[SIZE];
int grade[SIZE];
int a,b; // index variables
int student_id, grades;
// opening file by asking the user for the file name
printf("Enter the name of the file containing the grades\n");
scanf("%s",file_name);
fp=fopen("file_name", "r");
/*fp = fopen("grades.txt", "r");*/
// read in data into the arrays
for (a = 0; a <= SIZE; a++)
{
fscanf(fp,"%d", &student_id);
ID[a] = student_id;
for(b = 0; b <= SIZE; b++)
{
fscanf(fp,"%d", &grades);
grade[b] = grades;
}
if(ID[a] == 0 && grade[b] == 0)
break;
}
fclose(fp);
return 0;
}
這看起來像功課 – rerun 2011-06-10 21:22:14
是文件名超過32個字符?另外,如果這是家庭作業,您應該這樣標記它。 – 2011-06-10 21:22:49