从txt排序结构化数据

时间:2015-01-09 22:56:50

标签: c arrays sorting structure

我的问题是从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;  

首先我写第59行

bubble_sort(stg.number,12);    
像这样。但它得到错误而不是编译。我用

更改了它
bubble_sort(stg->number,12);    

这已编译但停止工作并收到警告

格式化输出:
在功能&#39; main&#39;:
59 3 [警告]传递&#39; bubble_sort&#39;的参数1从没有强制转换的整数生成指针[默认启用]
16 6 [注意]预期&#39; int *&#39;但参数的类型为&#39; int&#39;

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  

3 个答案:

答案 0 :(得分:0)

好吧,你的编译器输出告诉它它是:

你的bubble_sort函数接受一个指针,但是你把它变成一个整数。

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

答案 1 :(得分: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;
}

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

答案 2 :(得分: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