如何安排.txt文件的数据与C中其他数据的每个值相对应

How to arrange the data of a .txt file for correspond with each value of the other data in C

我的问题是我有一个包含数据的 txt 文件名 file.txt。我需要输入这个来做一些计算,这就是代码 运行:

Please enter input file name
file.txt
File file.txt
STUDENT NAME STUDENT NO.  SUBJECT A    SUBJECT B    
JOAN             1           70.5           85.0
TANIA            2           49.0           75.0
TOM              3           53.0           54.0
JEFF             4           80.0           49.5
SUSAN            5           89.0           90.0
KATHY            6           99.0           55.0
RAYMOND          7           22.5           75.0

这是文件中给出的数据。因此,例如 如果我按升序排列 Subject_A 列。 Column Name 和 Subject_B 没有安排 与 Subject_A 对应,代码如何继续:

Please enter a command (enter h for help): h
a/1 to obtain all the students that got diploma
b/2 to arrange subject A in ascending order
c/3 to calculate the average and standard deviation
d/4 to save all the above results in an output file
e to exit
2
Subject A in ascending order
STUDENT NAME SUBJECT A    SUBJECT B    
JOAN             22.5        85.0
TANIA            49.0        75.0
TOM              53.0        54.0
JEFF             70.5        49.5
SUSAN            80.0        90.0
KATHY            89.0        55.0
RAYMOND          99.0        75.0

这是我的问题,第一个是 22.5,对应于 Raymond(在上面给出的 .txt 中)和音符 B 75,在我得到的程序中是 Joan,b 中的音符为 22.5 和 85。 因为我只订购了A其余数据不对应。 每个学生都必须与她的学科成绩相对应

这是我的订购主题 A 的代码,但不是订购每个学生通讯员的每一行的代码:

void option_ordering_A(){
    while ( !feof( ptrCf ) ) {
        //printf( "%.1f\n", subject_A );
        a[i]=subject_A;
        fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
        i=i+1;
        } /* end while */
          for (i = 0; i < (SIZE - 1); i++) 
          { 
            for (j = i + 1; j < SIZE; j++) 
            { 
              if (a[j] < a[i]) 
              { 
                temp = a[j]; 
                a[j] = a[i]; 
                a[i] = temp; 
              }
            } 
          }
          /* Show Numbers Sorted */
          rewind( ptrCf );

          printf( "%-10s%-13s%-13s\n", "\nSTUDENT NAME ", "SUBJECT A" , "SUBJECT B");
          fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
          for (i = 0; i < SIZE; i++) 
          { 
            printf( "%-13s%8.1f%12.1f\n", student_name, a[i], subject_B );
            fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
            //printf("%.1f, ", a[i]); 
          } 
          printf("\n\n\n");

}

编辑:变量声明和函数。

float a [SIZE];
float b [SIZE];
int j;
int i = 0;
float temperature;
char student_name [30];
character arrays typedef [30];
matrices c [SIZE];
int student_No;
double subject_A;
double subject_B;

和函数部分:

//////////////////////FUNTIONS//////////////////////////////////////////////////////////////

/* Read the student's name, student's number, Subject A and Subject B */
void Read_File(){
printf( "%-10s%-13s%-13s%-13s\n", "\nSTUDENT NAME ", "STUDENT NO.", "SUBJECT A" , "SUBJECT B");
fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
}

////////////////////////////////////7//Show File///////////////////////////
void show_File(){
while ( !feof( ptrCf ) ) {
printf( "%-13s%5d%15.1f%15.1f\n", student_name, student_No, subject_A, subject_B );
fscanf( ptrCf, "%s%d%lf%lf", student_name, &student_No, &subject_A, &subject_B );
} /* end while */
}

 

评论区已经说过了吗,你在代码中使用结构体会容易很多。类似的东西(您可以根据需要更改它):

typdef struct Student {
    char student_name[30];
    int student_No;
    double subject_A;
    double subject_B;
};

在函数中创建一个结构变量:Student s;

然后当您调用 scanf 函数时,您可以这样调用它:fscanf(ptrCf, "%s%d%lf%lf", s.student_name, &s.student_No, &s.subject_A, &s.subject_B);

在函数 option_ordering_A() 中,您创建了一个结构数组,而不仅仅是针对主题 A

之后,您可以按主题 A 对结构数组进行排序,所有相关数据也会随之改变。