打印出一组结构

时间:2016-12-04 18:36:33

标签: c arrays struct

在这个程序中,我想要读取records.ssv的文件并使用结构数组打印出来。但是,在打印出阵列之前要计算平均值。该文件包含学生姓名,ID,考试1,考试2,项目1,项目2,平均和成绩(字母,单个)。

目前在打印出eh数组和计算平均值时遇到问题。我将在下面发布我的代码和输出。

fgets()

输出

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

typedef struct {
        char name[25];
        int I_D[9];
        int exam[2];
        int project[2];
        char grade;
        float avg;
} STUDENT;

void printStuAry(int size, STUDENT stuAry[]);

int main(void)
{
    STUDENT stuAry[5];

    FILE* f = fopen("records.ssv", "r");
    if (f == NULL) {
            printf("Error opening file records.ssv.\n");
            exit(1);
    }

    char line[65];
    int ind = 0;

    while (fgets(line, sizeof(line), f) != NULL) {
        sscanf(line, "%25[^,] ; %d ; %d ; %d ; %d ; %d ; %c",
               stuAry[ind].name,
               &stuAry[ind].I_D,
               &stuAry[ind].exam[0],
               &stuAry[ind].exam[1],
               &stuAry[ind].project[0],
               &stuAry[ind].project[1],
               &stuAry[ind].grade);
        ind++;
    }
    // calculates average
    stuAry[ind].avg = (stuAry[ind].exam[0] + stuAry[ind].exam[1] +
                       stuAry[ind].project[0] + stuAry[ind].project[1]) /4.0;

         printf("\t+---------+-------+-------+-------+---------+----------+----------+------+\n");
    printf("\t| Index   |Student| ID    | Exam1 | Exam2   |Project1  | Project2 | Grade|\n");
    printf("\t+---------+-------+-------+-------+---------+----------+--------+--------+\n");

    printStuAry(5, stuAry);
    if (fclose(f) == EOF) {
        printf("Error closing file records.ssv.\n");
        exit(2);
    }

    int letter;
    printf("Enter grade letter to search and q to quit");
    scanf("%d", letter);

    while (letter != -1){// tests for whether user wants to end program
        printStuAry(5, stuAry);
    }

    return 0;
}

/*
*Function name: printStuAry
*
*Input Parameters: int size Student Ary
*
*Desription: prints out student Ary
*                                                                
*Return value: 0
*/

void printStuAry(int size, STUDENT stuAry[])
{
        for (int i=0; i<size; i++) {
                printf("\t|  %d     |  %s   | %d    |  %d   |  %d     |   %d    |   %d     |  %c  |\n", //displays student information
                                         i, stuAry[i].name, stuAry[i].exam[0],
                                         stuAry[i].exam[1], stuAry[i].project[0],
                                         stuAry[i].project[1], stuAry[i].avg,
                                         stuAry[i].grade);
        }
}

正确的输出应该是什么:

原件:

        +---------+-------+-------+-------+---------+----------+----------+------+
        | Index   |Student| ID    | Exam1 | Exam2   |Project1  | Project2 | Grade|
        +---------+-------+-------+-------+---------+----------+--------+--------+
        |  0     |  Panzer   | -544760936    |  32568   |  -544762280     |   32568    |   -27     |     |
        |  1     |  Basler   | -1718237240    |  32767   |  -544747128     |   32568    |   -104     |     |
        |  2     |  Leaton   | -1718237400    |  32767   |  118     |   0    |   9     |     |
        |  3     |  Bishop   | 4195301    |  0   |  194     |   0    |   62     |     |
        |  4     |  Lucey   | -1718237064    |  32767   |  4197349     |   0    |   -96     |     |
Enter grade letter to search and q to quit

2 个答案:

答案 0 :(得分:0)

尝试将sscanf(line, "%25[^,] ; %d ; %d ; %d ; %d ; %d ; %c",替换为sscanf(line, "%25[^,] %d %d %d %d %d %c",

答案 1 :(得分:0)

typedef struct {
        char name[25];
        int I_D[9];
        …
} STUDENT;

您似乎已将char I_D[9]int I_D混为一谈;根据显示的样本数据,标识N和8位数字组成;您是否想要将其存储为字符串(char [9]太短)或整数(不包含N),您必须下定决心;让我们假设后者。

        sscanf(line, "%25[^,] ; %d ; %d ; %d ; %d ; %d ; %c",
     

但是文件数据用分号分隔......

然后sscanf在以下方面有误:

  • 25 - 由于您定义了char name[25],因此24个字符加上终止\0只有空间。
  • [^,] - 由于文件数据以分号分隔,因此必须为[^;]
  • ; %d - 要将整数部分读入I_D,必须使用之前的N

修正:

        sscanf(line, "%24[^;];N%d;%d;%d;%d;%d;%c",