小编daw*_*ose的帖子

如何使用结构正确地将文件内容传递给qsort?

我正在尝试让程序读取一个文本文件,其中包含"1001姓氏姓氏10 20 30"等信息,学生人数,姓名,姓氏和3年级.这是我的代码:

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 struct Student {
     int number;
     char name[30];
     char surname[30];
     int midterm1,midterm2,midterm3;
 } Student;

 int comp(const void * aa, const void * bb)
 {
    struct Student a = *(struct Student*)aa;
    struct Student b = *(struct Student*)bb;
    if (a.midterm1==b.midterm1)
            return 0;
    else if (a.midterm1 < b.midterm1)
            return -1;
    else
            return 1;
  } // comp


  int main(void)
  {
      int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
      char surname;
      FILE *cfPtr;

      struct student *name;
      name = malloc( 10 * sizeof(Student));

      if …
Run Code Online (Sandbox Code Playgroud)

c arrays struct

1
推荐指数
1
解决办法
455
查看次数

标签 统计

arrays ×1

c ×1

struct ×1