2015-01-09 100 views
0

我的问题是排序从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 
+0

你的'bubble_sort'函数会对'int'数组进行排序,但是你想排序一个'struct student_grades'数组。这是不可能的。您必须创建一个可以对'struct student_grades'数组进行排序的冒泡排序函数。请注意,交换会有点困难,因为您必须交换结构。 – wimh 2015-01-09 23:04:10

+1

虽然在这段代码中修复了许多其他错误,但可能会解决这个问题:['while(!feof(stu))'](http://stackoverflow.com/questions/5431941/while-feof-file-is-总是错误的)。 – WhozCraig 2015-01-09 23:23:58

+0

@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

回答

0

这是一种基于文件和您的结构stgrade DAND其使用快速排序与复杂度的代码等于O(log(n))qsort功能),而不是从库stdlib.h冒泡排序并产生期望的输出

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> //qsort function 

struct student_grades{ 
    int number; 
    char name[10]; 
    char surname[10]; 
    int grade; 
}; 

typedef struct student_grades stgrade; 

// the compare function is used by the qsort function to sort the structures 
// according to the grades values 
int compare(const void* a, const void* b) 
{ 
return (*(stgrade *)a).grade - (*(stgrade *)b).grade; 
} 

int main(int argc, char *argv[]) 
{ 
    FILE *stub, *stu; 
    stu= fopen("student.txt","r"); 
    stub= fopen("stu_order.txt","a"); 

    stgrade tab[12]; // array that will contain the structures from student.txt file 
    int i=0; 
    for (i=0;i<12;i++) 
    { 
     // put the structures on the array 
     fscanf(stu,"%d %s %s %d",&tab[i].number,tab[i].name,tab[i].surname,&tab[i].grade); 
    } 
     // use the qsort function that will sort the structures 
    qsort(tab,12,sizeof(stgrade),compare); 

    //loop to write the result on the output file 
    for (i=0;i<12;i++) 
    { 
     // the write will be via the function fprintf 
     fprintf(stub,"%d %s %s %d\n",tab[i].number,tab[i].name,tab[i].surname,tab[i].grade); 
    } 

    // Check the output file :) 

    return 0; 
} 

通过检查打开的文件可以改善代码!

+0

非常感谢!我知道我的流程不适合冒泡排序,我们使用快速排序而不是冒泡排序。而你使用for循环而不是while。我的老师说使用!feof()可以防止错误。但是如果我们使用静态数组,它不是问题。 再次感谢:)在土耳其我们说“Eyvallah”:) – 2015-01-09 23:45:26

+0

不要在你的下一个程序中使用'while(!feof(file))'这里是为什么[link](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)!!不用谢 ! – 2015-01-09 23:48:30

+0

有趣的老师说使用'!feof()'防止错误。老师是错的。像fscanf()这样检查IO函数的结果是一个更好的解决方案。 – chux 2015-01-09 23:49:10

0

那么,你的编译器的输出告诉它喜欢它是:

bubble_sort功能需要一个指针,但你把它的整数。

指针,地址,数组和整数都是重要的C概念,恐怕你必须刷新你的基本C语言知识;阅读一些参考代码可以帮助你理解如何解决你的问题。我知道你很可能是C的新手,这个答案是一个答案,但不能立即解决你的问题,但是这里解释的东西太多了,如果有其他人出现,它也不会真正帮助你将您的问题标记为低质量。

+1

传递给'fscanf'的值的格式说明符正确(主要)。 '%s'需要'char *',当名称和参数表达为参数时,'name'和'surname'成员都会转换为。如果OP的'fscanf'代码有任何问题,(a)未能检查成功/失败的结果,以及(b)未能将'%s'格式说明符限制为正在传递的字符缓冲区的大小。代码中有很多错误。为什么你挑选出正确的东西(像OP所做的那样传递'name'和'surname')看起来很奇怪。 – WhozCraig 2015-01-09 23:20:13

+1

“你为什么认为有时在fscanf中使用变量的地址(使用&前缀运算符)并且有时不使用?” - > C规范这样做'int n,i; float x; char name [50]; n = fscanf(stdin,“%d%f%s”,&i,&x,name);'你是否在暗示C规范和OP是错误的? – chux 2015-01-09 23:24:11

+0

不,chux,这表明你不明白'char name [50]'使'name'成为'char *',这是一个指向数组中第一个'char'的指针。再次,这是非常基本的C! 虽然你绝对正确,但那是我的措辞。我很抱歉!我试图提出的一点是,如果他在数组上操作,他必须意识到什么是指针,什么是对象本身。 – 2015-01-09 23:26:15

0
there were lots of problems with the code 
however the following should have all those problems corrected 
and includes error checking 


#include <stdio.h> 
#include <stdlib.h> // exit 
#include <string.h> // memcpy 

#define MAX_GRADES (12) 

struct student_grades 
{ 
    int number; 
    char name[10]; 
    char surname[10]; 
    int grade; 
}; 




void bubble_sort(struct student_grades* pList, int n) 
{ //Line 16 
    long c, d; 
    struct student_grades t; 

    for (c = 0 ; c < (n - 1); c++) 
    { 
     for (d = 0 ; d <(n - c - 1); d++) 
     { 
      if (pList[d].number > pList[d+1].number) 
      { 
       /* Swapping */ 

       memcpy(&t, &pList[d], sizeof(struct student_grades)); 
       memcpy(&pList[d], &pList[d+1], sizeof(struct student_grades)); 
       memcpy(&pList[d+1], &t, sizeof(struct student_grades)); 
      } // end if 
     } // end for 
    } // end for 
} // end function: bubble_sort 


int main() 
{ 
    FILE *stu = NULL; // file tipinde değişken tutacak 
    FILE *stub = NULL; // rises compiler warning about unused variable 

    if(NULL == (stu= fopen("student.txt","r"))) 
    { // then, fopen failed 
     perror("fopen for student.txt failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, fopen successful 

    if(NULL == (stub= fopen("stu_order.txt","a"))) 
    { // then, fopen failed 
     perror("fopen for stu_order.txt failed"); 
     fclose(stu); // cleanup 
     exit(EXIT_FAILURE); 
    } 

    // implied else, fopen successful 


    struct student_grades stg[MAX_GRADES]; 
    char line[1000]; // should be enough for reading 4 fields 

    int i=0; 
    while((i<MAX_GRADES) && fgets(line, sizeof(line), stu)) 
    { 
     if(4 != sscanf(line," %d %s %s %d", 
         &stg[i].number, 
         stg[i].name, 
         stg[i].surname, 
         &stg[i].grade)) 
     { // fscanf failed 
      perror("fscanf failed"); 
      fclose(stu); // cleanup 
      fclose(stub); 
      exit(EXIT_FAILURE); 
     } 

     // implied else, fscanf successful 

     //fprintf(stub,"%d %s %s %d\n",stg[i].number,stg[i].name,stg[i].surname,stg[i].grade); 

     ++i; 
    } // end while 

    bubble_sort(stg,i); //Line 59 

    int j; 
    for(j=0;j<i;j++) 
    { 
     fprintf(stub,"%d %s %s %d\n", 
       stg[1].number, 
       stg[1].name, 
       stg[1].surname, 
       stg[1].grade); //control that is bubble success? 
    } // end for 

    fclose(stu); // leanup 
    fclose(stub); 
    return 0; 
} // end function: main 
+0

C中支持通过赋值进行本机结构复制。'memcpy'不是必需的。 – WhozCraig 2015-01-10 08:07:19