我在C中遇到了struct的問題,任何人都可以幫忙嗎?我是C新手,所以請對我好。我已經在下面聲明瞭兩個結構,其中一個嵌套在另一個結構中。從文件初始化嵌套結構的值
struct orders_tag {
int number_of_orders;
char item_name[20];
int price;
};
typedef struct orders_tag order;
struct customer_tag {
char name[30];
order total_order[10];
};
typedef struct customer_tag customer;
我也在main中初始化了這些值。
customer cus_array[20];
customer c;
我想從文件中讀取信息,並把這些值代入我的嵌套的結構,但我真的不明白是怎麼回事。任何解釋將不勝感激。
infile = fopen("input.txt", "r");
if (infile == NULL) {
printf("Couldn't open the fire.");
return 1;
}
while (fscanf(infile, "%s %d %s %2f", c.name, c.total_order.number_of_orders
, c.total_order.item_name, c.total_order.price) != EOF) {
}
我相信我得到的唯一錯誤是來自while循環條件。
'total_order'是一個數組。所以這個'c.total_order.item_name'和這個'c.total_order.price'不會被編譯。 – alk 2015-02-06 09:19:26
謝謝,我會認爲我會做一些本地臨時變量,並將文件中的值放入這些臨時變量中,然後從那裏手動初始化該結構。 – ln206 2015-02-06 09:21:50