Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从txt排序结构化数据_C_Arrays_Sorting_Structure - Fatal编程技术网

C 从txt排序结构化数据

C 从txt排序结构化数据,c,arrays,sorting,structure,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 stgrad

我的问题是对从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;  
像这样。但它会出错,无法编译。我把它换成

bubble_sort(stg->number,12);    
此文件已编译,但已停止工作并收到警告

格式化输出: 在函数“main”中: 59 3[警告]传递'bubble_sort'的参数1会使指针从整数变为不带强制转换的指针[默认情况下启用] 16 6[注意]应为“int*”,但参数的类型为“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_排序函数接受一个指针,但你给它一个整数


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

这是一个基于您的文件和结构stgrade的代码,它使用的快速排序的复杂性等于Ologn 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;
}

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

您的bubble\u sort函数将对int数组进行排序,但您希望对struct student\u grades数组进行排序。这是不可能的。您必须创建一个气泡排序函数,该函数可以对struct student_grades数组进行排序。请注意,交换将有点困难,因为您必须交换结构。在修复此代码中大量其他错误的同时,也许还可以修复此问题。@WhozCraig,我想您找到了。OP:一会儿!feofstu{fscanfstu,%d,&stg[i].number;…fscanfstu,%d,&stg[i].grade;->而4==fscanfstu,%d%9s%9s%d,&stg[i].number,stg[i].姓名,stg[i].姓氏,&stg[i].grade{与传递给fscanf的值一起使用的格式说明符大部分是正确的。%s需要char*,名称和姓氏成员在表示为参数时都将转换为char*。如果OP的fscanf代码有任何错误,是否是a未能检查结果是否成功/失败,b未能将%s格式说明符限制为t正在传递的字符缓冲区的大小。代码中有很多错误。为什么你像OP那样挑出传递名称和姓氏正确的东西似乎有些奇怪。为什么你认为有时使用&prefix运算符在fscanf中使用变量的地址是可以的,而有时不可以?->C规范就是这样做的,i;float x;char name[50];n=fscanfstdin,%d%f%s,&i,&x,name;您是否认为C规范和OP是错误的?不,chux,这表明您不理解该char name[50]使名称成为一个字符*,一个指向数组中第一个字符的指针。同样,这是一个非常基本的C!但你绝对正确,我的措辞很糟糕。我很抱歉!我想表达的意思是,如果他在数组上操作,他必须知道什么是指针,什么是对象本身。非常感谢!我理解这一点我的进程不适用于冒泡排序,我们使用快速排序而不是冒泡排序。你使用for循环而不是while。我的老师说使用!feof可以防止错误。但是如果我们使用静态数组,这不是问题。再次感谢:在土耳其语中,我们说Eyvallah:不要在下一个程序中使用while!feoffile,原因如下!!你是欢迎!有趣的老师说使用!feof可以防止错误。老师错了。检查像fscanf这样的IO函数的结果是一个更好的解决方案。我想他说对于动态数组,因为它可以扫描缓冲区中的一些文件。如果你不知道文件中有多少行,这将是一个很好的解决问题!首先t您可以做一个循环来计算您有多少行,然后为动态数组分配所需的内存!然后您需要再次指向文件的开头,并执行与上面答案相同的操作!C中支持通过赋值复制本机结构。memcpy不是必需的。
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
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