读取 table 格式的文件并打印

Reading a file in a table format and print it

所以,我要做的是,首先创建一个 table 格式的文件,然后读取该文件并将该文件放入 4 个不同的动态数组 使用 struct 并按顺序打印它们

所以,这是我正在使用的结构:我使用 capacity = 5 稍后更改动态数组的大小,但仍然不知道如何更改它

struct score{
    char *str1;
    char *str2;
    int *num1;
    int *num2;
};

int main(){
    int capacity = 5;
    struct score table;
    table.str1=(char *)malloc(sizeof(int)*capacity);
    table.str2=(char *)malloc(sizeof(int)*capacity);
    table.num1=(int *)malloc(sizeof(int)*capacity);
    table.num2=(int *)malloc(sizeof(int)*capacity);

在我创建了一个 File 之后 write:

    inFile = fopen("Subject.txt", "w");
    if(inFile == NULL)
    {
      printf("Error!");   
      exit(1);             
    }

    char names[] = {"Joe Math 52 85\nBilly Phy 65 70\nSophia Chem 86 71"};

    fprintf(inFile,"%s",names);
    fclose(inFile);

最后读取一个文件并将它们放入数组中:

    inFile = fopen("Subject.txt", "r");
    if(inFile == NULL)
    {
      printf("Error!");   
      exit(1);             
    }
    fscanf(inFile, "%s %s %d %d",table.str1, table.str2, table.num1, table.num2);
    fclose(inFile);

    for(i=0;i<6;i++){
        printf("%s %s %d %d\n",table.str1, table.str2, table.num1, table.num2);
    }

所以,我需要这段代码来像这样打印,但我做不到:

Name   Subj.  Test1  Test2
------------------------
Joe    Math   52     85
Billy  Phy    65     70
Sophia Chem   86     71

这是我的完整代码以防万一:

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

struct score{
    char *str1;
    char *str2;
    int *num1;
    int *num2;
};

int main(){
    FILE *inFile;
    int num, capacity = 5, i;
    struct score table;
    table.str1=(char *)malloc(sizeof(int)*capacity);
    table.str2=(char *)malloc(sizeof(int)*capacity);
    table.num1=(int *)malloc(sizeof(int)*capacity);
    table.num2=(int *)malloc(sizeof(int)*capacity);
    inFile = fopen("Subject.txt", "w");
    if(inFile == NULL)
    {
      printf("Error!");   
      exit(1);             
    }

    char names[] = {"Joe Math 52 85\nBilly Phy 65 70\nSophia Chem 86 71"};

    fprintf(inFile,"%s",names);
    fclose(inFile);

    inFile = fopen("Subject.txt", "r");
    if(inFile == NULL)
    {
      printf("Error!");   
      exit(1);             
    }
    fscanf(inFile, "%s %s %d %d",table.str1, table.str2, table.num1, table.num2);
    fclose(inFile);

    for(i=0;i<6;i++){
        printf("%s %s %d %d\n",table.str1, table.str2, table.num1, table.num2);
    }
}

我想你想要一个结构数组,每个学生都有一个数组元素。现在您正在使用一个结构来保存各个信息片段的数组。

数组看起来像:

#define MAX_NAME 30
#define MAX_STUDENTS 10

struct score{
    char str1[MAX_NAME];
    char str2[MAX_NAME];
    int num1;
    int num2;
};

struct score table[MAX_STUDENTS];

有了这个定义,你就不需要 malloc 内存了(它更简单,但不灵活)。

您将信息读入数组为:

int i=0;
while (fscanf(inFile, "%30s %30s %d %d",table[i].str1, table[i].str2,
  &table[i].num1, &table[i].num2) == 4) {
    i++;
}

你想要一个数组结构。

但是,您的分数是固定的(例如 num1num2)。这也应该是一个数组。

我认为使用 second 结构会更好。即 struct studentstruct score

而且,Paul 概述了如何使用固定限制来做到这一点。

一般来说,您可以动态地分配事物以容纳任意数量的学生,这些学生的考试分数是任意(且不同)的。

根据您的示例数据,您的输入格式为:

<name> <subject> <score1> <score2> ... <scoreN>

因此,我将 subject 解释为学生 专业 ,因此它在 student 记录中分组。

否则,我们需要这样的东西:

<name> <subject1> <score1> <subject2> <score2> ... <subjectN> <scoreN>

然后,subject 将进入 score 记录


为了让它工作,我不得不[大量]重构你的代码:

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

struct score {
    int score;
};

struct student {
    char *name;
    char *subject;
    struct score *scores;
    int count;
};

int
main(void)
{
    FILE *inFile;
    struct student *students = NULL;
    int student_count = 0;
    struct student *who;
    struct score *score;
    const char *delim = " \t\n";
    char *cp;
    char buf[1000];

    inFile = fopen("Subject.txt", "w");
    if (inFile == NULL) {
        printf("Error!");
        exit(1);
    }

    char names[] = { "Joe Math 52 85\nBilly Phy 65 70\nSophia Chem 86 71" };

    fprintf(inFile, "%s", names);
    fclose(inFile);

    inFile = fopen("Subject.txt", "r");
    if (inFile == NULL) {
        printf("Error!");
        exit(1);
    }

    while (1) {
        // get a line
        cp = fgets(buf,sizeof(buf),inFile);
        if (cp == NULL)
            break;

        // get the student name
        cp = strtok(buf,delim);
        if (cp == NULL)
            continue;

        // allocate new student record
        students = realloc(students,
            sizeof(struct student) * (student_count + 1));
        who = &students[student_count];
        student_count += 1;

        // save the student name
        who->name = strdup(cp);

        // get the subject and save it
        cp = strtok(NULL,delim);
        if (cp == NULL)
            break;
        who->subject = strdup(cp);

        // clear out the scores array
        who->count = 0;
        who->scores = NULL;

        // get all scores
        while (1) {
            cp = strtok(NULL,delim);
            if (cp == NULL)
                break;

            // increase the size of the scores array for this student
            who->scores = realloc(who->scores,
                sizeof(struct score) * (who->count + 1));
            score = &who->scores[who->count];
            who->count += 1;

            score->score = atoi(cp);
        }
    }

    fclose(inFile);

    for (who = &students[0];  who < &students[student_count];  ++who) {
        printf("%10s %10s", who->name, who->subject);

        for (score = who->scores;  score < &who->scores[who->count];  ++score)
            printf("%4d",score->score);

        printf("\n");
    }

    return 0;
}

程序输出如下:

       Joe       Math  52  85
     Billy        Phy  65  70
    Sophia       Chem  86  71