即时试图从一个文件的文件的设置是这样读出的数据:将数据读入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