我的问题是排序从txt文件中取得的一些数字。 当我编译文件时,程序停止工作。从txt排序结构化数据
#include <stdio.h>
#include <stdlib.h>
struct student_grades{
int number;
char name[10];
char surname[10];
int grade;
};
typedef struct student_grades stgrade;
void bubble_sort(int list[], int n){ //Line 16
long c, d, t;
for (c = 0 ; c < (n - 1); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
/* Swapping */
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
int main()
{
int i=0;
FILE *stu; // file tipinde değişken tutacak
FILE *stub;
stu= fopen("student.txt","r");
stub= fopen("stu_order.txt","a");
stgrade stg[12];
if(stu!=NULL){
while(!feof(stu))
{
fscanf(stu,"%d",&stg[i].number);
fscanf(stu,"%s",stg[i].name);
fscanf(stu,"%s",stg[i].surname);
fscanf(stu,"%d",&stg[i].grade);
//fprintf(stub,"%d %s %s %d\n",stg[i].number,stg[i].name,stg[i].surname,stg[i].grade);
++i;
}
bubble_sort(stg->number,12); //Line 59
fprintf(stub,"%d %s %s %d\n",stg[1].number,stg[1].name,stg[1].surname,stg[1].grade); //control that is bubble success?
}
else
printf("File Not Found");
fclose(stu);
fclose(stub);
return 0;
在第i个写线59
bubble_sort(stg.number,12);
这样。但它会得到错误,而不是编译。我改变它
bubble_sort(stg->number,12);
这一点,编译,但停止工作,并得到预警
格式化输出:
在功能“主”:
59 3【警告】通过参数1“bubble_sort”品牌从整数指针,未作施放[默认启用]
16 6 [注]预期 '诠释*',但参数的类型 'INT' 的
的student.txt
80701056 Sabri Demirel 45
52801022 Burak Erkin 68
13801045 Umut Korkmaz 88
74801334 Semih Potuk 58
15678544 Enes Sezer 76
42125884 Ahmet Can 84
12355488 Emre Ozdemir 47
18744125 Ugur Yildiz 64
62184111 Mustafa Ozturk 80
18412548 Ugur Akkafa 72
94541771 Hakan Aktas 92
36945245 Fatih Yagci 98
你的'bubble_sort'函数会对'int'数组进行排序,但是你想排序一个'struct student_grades'数组。这是不可能的。您必须创建一个可以对'struct student_grades'数组进行排序的冒泡排序函数。请注意,交换会有点困难,因为您必须交换结构。 – wimh 2015-01-09 23:04:10
虽然在这段代码中修复了许多其他错误,但可能会解决这个问题:['while(!feof(stu))'](http://stackoverflow.com/questions/5431941/while-feof-file-is-总是错误的)。 – WhozCraig 2015-01-09 23:23:58
@WhozCraig我想你明白了。 OP:'while(!feof(stu)){fscanf(stu,“%d”,&stg [i] .number); ... fscanf(stu,“%d”,&stg [i] .grade);' - >'while(4 == fscanf(stu,“%d%9s%9s%d”,&stg [i]。编号,stg [i] .name,stg [i] .surname,&stg [i] .grade)){' – chux 2015-01-09 23:26:38