首页 > 解决方案 > 读取表格格式的文件并打印

问题描述

所以,我想做的是,首先以表格格式创建一个文件,然后读取该文件并将该文件放入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);

在我创建了一个要写入的文件之后:

    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);
    }
}

标签: cfilestructdynamic-arrays

解决方案


I think you want an array of structs with one array element for each student. Now you are using one struct to hold arrays of the individual pieces of information.

The array would look like:

#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];

With this definition you don't need to malloc memory (it is simpler, but not flexible).

You read the information into the array as:

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

推荐阅读