【问题标题】:Reading from a File to an Array - C从文件读取到数组 - C
【发布时间】:2014-04-06 21:05:48
【问题描述】:
int main()
{

    FILE* infile1;

    int stockCode[15];
    char stockName[100];
    int stockQuantity[15];
    int stockReorder[15];
    int unitPrice[15];
    int i;

    infile1  = fopen("NUSTOCK.TXT", "r");

    while(fscanf(infile1, "%d %s %d %d %f",
         &stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF)
    {
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
          stockCode, stockName, stockQuantity, stockReorder, unitPrice);
    }

    fclose(infile1);

}

我要做的是从文件中获取信息并将其存储到 5 个单独的数组中。但是,在打印出来时,它只会正确打印出名称。

1394854864 修剪篮 1394854688 1394854624 0.00
1394854864 梨篮 1394854688 1394854624 0.00
1394854864 桃篮 1394854688 1394854624 0.00
1394854864 豪华塔 1394854688 1394854624 0.00

原始文件如下所示。所以所有的数字都没有被扫描,我不知道为什么......

101 修剪篮 065 060 25.00
105 梨篮 048 060 30.00
107 桃篮 071 060 32.00
202 豪华塔 054 040 45.00

【问题讨论】:

    标签: c arrays file file-io


    【解决方案1】:

    我认为你想做的是设计一个结构来保存许多个人记录。 每条记录包含:

    • 代码
    • 姓名
    • 数量
    • 重新排序
    • 单价

    你应该知道C语言中每种类型的含义。

    我建议你像这样重写你的代码:

    #include <stdio.h>
    #include <stdlib.h>
    struct OneRecord{
        int code;
        char name[100];
        int quantity;
        int recorder;
        float unitPrice;
    };
    
    int main(){
    
    
        struct OneRecord* records = (struct OneRecord*)calloc(15, sizeof(struct OneRecord));
        int i = 0;
    
        FILE* infile1  = fopen("NUSTOCK.TXT", "r");
        int max=0;
    
        //%99s is used for max string length, because of we can protect the out of string's memory length
        while((max<15)&&(fscanf(infile1, "%d %99s %d %d %f",
                    &records[i].code, records[i].name, &records[i].quantity, &records[i].recorder, &records[i].unitPrice) == 5))
        {
            i++;
            max++;
        }
        for(i=0;i<max;i++){
            printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                    records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
        }
    
        fclose(infile1);
        free(records);
    }
    

    以及如何在函数或许多其他地方使用 C Structure? 在 C 编程语言中,有 int、char、struct 等不同的类型。您可以像许多其他类型一样使用struct

    void printRecords(const struct OneRecord* records, int max)
    {
        int i;
        for(i=0;i<max;i++){
            printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                    records[i].code,records[i].name,records[i].quantity,records[i].recorder,records[i].unitPrice);
        }
    }
    

    【讨论】:

    • 未成年人:建议""%d %99s %d %d %f""while (fscanf(...) == 5)
    • 谢谢!我没想过使用结构,经过研究以了解更多信息后,我想我明白了。我的下一个问题是,我将如何在我编写的函数中使用该结构?
    • @user3357457 请参阅示例。您可以像许多其他类型一样使用struct
    【解决方案2】:

    您错误地使用了这些数组。试试这个:

    i = 0;
    while(fscanf(infile1, "%d %s %d %d %f",
                stockCode+i, stockName, stockQuantity+i, stockReorder+i, unitPrice+i) != EOF)
    {
        printf("  %3d  %-18s  %3d  %3d  %6.2f \n",
                stockCode[i], stockName, stockQuantity[i], stockReorder[i], unitPrice[i]);
        i++;
    }
    

    此外,unitPrice 应该是 float 的数组,而不是 int 的数组。

    【讨论】:

    • @chux 是的,你是对的。我忘了提。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多