【问题标题】:reading from a text file using fscanf()使用 fscanf() 从文本文件中读取
【发布时间】:2015-02-06 21:12:48
【问题描述】:

我无法弄清楚我的代码出了什么问题。我认为我的 while 循环没有正确读取文件,我试图打印出客户名称但没有任何显示。

例如,我有一个这样的文件。

Smith 3 Sweater $22.50
Reich 3 Umbrella $12.50
Smith 1 Microwave $230.00
Lazlo 1 Mirror $60.00
Flintstone 5 Plate $10.00
Lazlo 1 Fridge $1200.00
Stevenson 2 Chair $350.00
Smith 10 Candle $3.50
Stevenson 1 Table $500.00
Flintstone 5 Bowl $7.00
Stevenson 2 Clock $30.00
Lazlo 3 Vase $40.00
Stevenson 1 Couch $800.00

这是我的代码:

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

struct orders_tag {
    int number_of_orders;
    char item_name[20];
    double price;
};

typedef struct orders_tag order;

struct customer_tag {
    char name[30];
    order total_order[100];
};

typedef struct customer_tag customer;

int main(void) {
    FILE *infile;
    customer cus_array[20];
    customer c;
    int customerCounter = 0;

    setvbuf(stdout, NULL, _IONBF, 0);

    infile = fopen("input.txt", "r");

    if (infile == NULL) {
        printf("Couldn't open the fire.");
        return 1;
    }

    while (fscanf(infile, "%s %d %s %lf", c.name,  c.total_order[customerCounter].number_of_orders
        , c.total_order[customerCounter].item_name,  c.total_order[customerCounter].price) != EOF) {
        cus_array[customerCounter] = c;
        customerCounter++;
    }

    int j;
    for(j = 0; j < customerCounter; j++) {
        printf("%s", cus_array[j].name);
    }
    return 0;
}

【问题讨论】:

  • 除了将&amp; 用于int 和double,建议:不要比较fscanf(...) != EOF),而是使用fscanf(...) == 4)。这将终止扫描问题的循环,而不是陷入无限循环。 IOW,当代码获得所有它的输入时使用数据。

标签: c arrays struct scanf


【解决方案1】:

你的代码有很多问题。

第 1 点:您必须将 地址 [指针] 提供给 fscanf() 以存储值。改变

while (fscanf(infile, "%s %d %s %lf", c.name,  c.total_order[customerCounter].number_of_orders
    , c.total_order[customerCounter].item_name,  c.total_order[customerCounter].price) != EOF) {

到

while (fscanf(infile, "%s %d %s %lf", c.name,  &c.total_order[customerCounter].number_of_orders
    , c.total_order[customerCounter].item_name,  &c.total_order[customerCounter].price) != EOF) {

第2点:在你的输入文件中,每一行的输入是

Smith 3 毛衣 $22.50

因此,您必须将 fscanf() 格式更改为 "%s %d %s $%lf" 以匹配输入。因此,始终建议检查 fscanf() 和系列的重新运行值,以确保正确扫描所有值。

第 3 点: customerCounter 变量使用错误。

第 4 点:根据您的输入文件,total_order 不必是数组。

检查以下代码。它有效。

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

struct orders_tag {
        int number_of_orders;
        char item_name[20];
        double price;
};

typedef struct orders_tag order;

struct customer_tag {
        char name[30];
        order total_order;   //array not required
};

typedef struct customer_tag customer;

int main(void) {
        FILE *infile;
        customer cus_array[20];
        customer c;
        int customerCounter = 0;

        setvbuf(stdout, NULL, _IONBF, 0);

        infile = fopen("input.txt", "r");

        if (infile == NULL) {
                printf("Couldn't open the fire.");
                return 1;
        }

        while (fscanf(infile, "%s %d %s $%lf", c.name,  &c.total_order.number_of_orders
                                , c.total_order.item_name,  &c.total_order.price) != EOF) {//notice the changes here
                cus_array[customerCounter] = c;
                customerCounter++;
                if (customerCounter == 20) break; // memory allocated for only 20 elements
        }

        int j;
        for(j = 0; j < customerCounter ; j++) {
                printf("Customer :%10s, Number of order : %2d, Item : %10s, Price : $%f\n",
                        cus_array[j].name, cus_array[j].total_order.number_of_orders,cus_array[j].total_order.item_name, cus_array[j].total_order.price);
        }
        return 0;
}

【讨论】:

  • 奇怪,还是没有打印出客户的名字。
  • 感谢您解释我做错了什么,直到现在我都忙了一整天。很好解释,谢谢!
【解决方案2】:

除了其他答案之外,cus_array[customerCounter] = c; 行不会按照您的意愿行事。赋值是一个浅拷贝操作符,所以数组字段不会被正确拷贝。相反,您应该使用strcpy 和/或memcpy 显式复制字段。但更好的解决方案是使用指向结构的指针而不是结构本身。

【讨论】:

  • 这是否意味着我必须创建更多临时变量,然后从那里初始化结构?谢谢。
  • 我会说相反的。摆脱c 并直接使用数组元素。或者将c 设为customer* 类型,将&amp;cus_array[customerCounter] 分配给它,然后将您的数据读入c-&gt;whatever。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
相关资源
最近更新 更多