【问题标题】:Read *.data file into dynamically expanding array of struct***将 *.data 文件读入动态扩展的 struct 数组中***
【发布时间】:2015-02-17 05:03:57
【问题描述】:

我是 C 编程的新手,我想将 *.data 文件读入动态扩展的结构数组中。完成后,我想按值排序并按高/低顺序打印。我已经能够完成排序和打印功能,但不能完成文件的阅读。 我经历了几个例子,但无法理解。任何帮助,将不胜感激。谢谢

文件中的示例数据

alpha 4.8 28000 白色

delta 1.2 321 橙色

struct dataBase
{
    char modelName[31];
    float capacity;
    int mileage;
    char color[15];
};

int main (void)
{

readData();

// set up conditions for the loop

int choice;
choice = 0;

// I used a do while loop to insure that it would run once
do{
    // print menu
    printf ( "Make your choice.\n");
    printf ( "1. Sort data by the float value & print high to low.\n" );
    printf ( "2. Sort data by the float value & print low to high.\n" );
    printf ( "3. Sort data by the int value & print high to low.\n" );
    printf ( "4. Sort data by the int value & print low to high.\n");
    printf ( "5. Exit.\n\n");

    // get user input and clear out stdin
    choice = getchar();
    // this part clears the stdin of leftovers
    while ( getchar() != '\n' ) ;

    // Call the desired function
    if (choice == '1' ) {
        compareFloat();
        printHigh(Input_Size);
    }
    else if (choice == '2' ) {
        compareFloat();
        printLow(Input_Size);
    }
    else if (choice == '3' ) {
        compareInt();
        printHigh(Input_Size);
    }
    else if (choice == '4') {
        compareInt(Input_Size);
    }
}
// Check for escape code
while ( choice != '5');

// just to verify I finished
printf ( "Done\n");

return 0;         
}

int compareFloat(const void *s1, const void *s2)
{
  struct dataBase *e1 = (struct dataBase *)s1;
  struct dataBase *e2 = (struct dataBase *)s2;
  if (e1->capacity < e2->capacity)
    return -1;
  else if (e1->capacity > e2->capacity)
    return +1;
  else
    return 0;
}

void printLow(int size)
{
    for(int i=0; i<size; i++)
    {
        printf(dataBase[i].modelName + " " + dataBase[i].capacity + " " + 
               dataBase[i].mileage + " " + dataBase[i].color);
    }
}

void readData(void)
{
    FILE *inputFile
    inputFile = fopen("file.data", 'rb');
}

【问题讨论】:

    标签: c arrays dynamic structure readfile


    【解决方案1】:
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    
    struct dataBase
    {
        char modelName[31];
        float capacity;
        int mileage;
        char color[15];
    };
    
    struct dataBase *Inputs      = NULL;
    size_t           Inputs_Size = 0;
    size_t           Num_Inputs  = 0;
    
    void printLow(int size)
    {
        int i;
        for(i=0; i<size; i++)
        {
            printf("%s %f %d %s\n", Inputs[i].modelName, Inputs[i].capacity, Inputs[i].mileage, Inputs[i].color);
        }
    }
    
    void readData(void)
    {
        FILE *inputFile;
        struct dataBase input;
    
        if (NULL == (inputFile = fopen("file.data", "r")))
        {
            perror("Couldn't open file.data!");
            return;
        }
    
        while (4 == fscanf(inputFile, "%30s %f %d %14s", input.modelName, &input.capacity, &input.mileage, input.color))
        {
            if (++Num_Inputs > Inputs_Size)
            {
                size_t new_size = 2 * Num_Inputs;
                struct dataBase *new_inputs = realloc(Inputs, new_size * sizeof(struct dataBase));
    
                if (NULL == new_inputs)
                {
                    perror("realloc failed!");
                    goto FAIL_INPUTFILE;
                }
    
                Inputs      = new_inputs;
                Inputs_Size = new_size;
           }
    
           Inputs[Num_Inputs - 1] = input;
           printf("'%s' %f %d '%s'\n", input.modelName, input.capacity, input.mileage, input.color);
        }
    
        if (!feof(inputFile))
        {
            fprintf(stderr, "Malformed line didn't match expected input format!\n");
        }
    
    FAIL_INPUTFILE:    
        fclose(inputFile);
    }
    
    int main()
    {
      readData();
    
      return 0;
    }
    

    【讨论】:

    • 但是结构将如何动态扩展呢?以及如何将条目添加到集合中?
    • 啊,我还以为你的问题出在 I/O 上。给我一点时间来更新我的答案。
    • 这段代码非常适合我的 I/O。您是否也可以查看我的其他代码,它们会按预期工作吗?
    • 你的容量比较 fcn 看起来不错。您的 printLow 没有。 C 不像 Java 那样使用 + 运算符连接字符串。我也会更新我的答案。
    • 你不想调用你的比较函数,它只比较两个条目。您需要调用一个排序函数,该函数将使用您的比较函数对输入进行排序。 Google 函数 qsort(),它是标准库的一部分,并在 stdlib.h Feed 中声明您的比较器函数和数组正确,它将为您排序。如果您需要编写自己的排序函数,那么您需要做更多的工作。 :)
    猜你喜欢
    • 2014-02-01
    • 2016-12-10
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 2021-10-26
    • 2021-11-26
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多