gpt4 book ai didi

c - 从文件读取变量并将它们存储在结构中 - C

转载 作者:行者123 更新时间:2023-11-30 15:14:26 25 4
gpt4 key购买 nike

我在使用这个程序时遇到了一些严重的问题。我几乎需要从一个包含一组名字和姓氏、cookie 类型、购买金额、成本以及是否付费的文件中读取内容。

我相当有信心我的功能是正确的。

当我运行该程序时,它运行正确,但某些数字之间似乎有多余的数字。我的意思是,如果输入文件是:约翰·格林 薄荷 1 1.00 是简蓝芯片 2 2.00 N输出文件将类似于:约翰·格林 12 20000 完好 455 1 等...

有什么想法吗?我猜我在读取文件/将它们存储到结构中时搞砸了。

#include <stdio.h>
#include <ctype.h>

/*structure definition*/
struct customer{
char first[12], last[12], type[10], paid;
int boxes;
float cost;
};

/*prototypes*/
void readCustomerData(FILE*, struct customer[]);
void printCustomerData(FILE*, int, struct customer[]);
float moneyReceived(int, struct customer[]);
int custWhoOwe(int, struct customer[]);
float moneyOutstanding(int, struct customer[]);
int moneyReceivedPercent(int, struct customer[]);

int main (){
int numCustomers;
float money, money2;
int cust, perc;
FILE*input;
FILE*output;

input = fopen("custInfo.txt", "r");
output = fopen("output.txt", "w");

fscanf(input, "%d", &numCustomers);


struct customer customerData[numCustomers];

readCustomerData(input, customerData);
printCustomerData(output, numCustomers, customerData);
fprintf(output, "Summary:\n\n");
money = moneyReceived(numCustomers, customerData);
fprintf(output, "Total Cash Received: $%.2f\n\n", money);
cust = custWhoOwe(numCustomers, customerData);
fprintf(output, "Num customers who have not paid: %d\n\n", cust);
money2 = moneyOutstanding(numCustomers, customerData);
fprintf(output, "Total Amount Outstanding: $%.2f\n\n", money2);
perc = moneyReceivedPercent(numCustomers, customerData);
fprintf(output, "Percentage of Sales outstanding: %d%%\n\n", perc);



fclose(input);
fclose(output);
return 0;
}

void readCustomerData(FILE*file, struct customer cdata[]){
int numCustomers, i;
file = fopen("custInfo.txt", "r");

fscanf(file, "%d", &numCustomers);

for (i=0; i<numCustomers; i++){
fscanf(file, "%s%s%s%d%f%c", &cdata[i].first, &cdata[i].last, &cdata[i].type, &cdata[i].boxes, &cdata[i].cost, &cdata[i].paid);
}
fclose(file);
}

void printCustomerData(FILE*file, int num, struct customer cdata[]){
int i;
fprintf(file, "Customers:\n\n");
fprintf(file, "Name\t\tType of Cookie\t\tNum of Boxes\tCost of Box\tPaid\n");
fprintf(file, "--------------------------------------------------------------------------------\n\n");
for (i=0; i<num; i++){
fprintf(file, "%s %s\t", cdata[i].first, cdata[i].last);
fprintf(file, "%s\t\t", cdata[i].type);
fprintf(file, "%d\t\t", cdata[i].boxes);
fprintf(file, "$%.2f\t\t", cdata[i].cost);
fprintf(file, "%c", cdata[i].paid);
fprintf(file, "\n\n");
}
fprintf(file, "\n\n\n\n\n");

}

float moneyReceived(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if(cdata[i].paid=='Y'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}

int custWhoOwe(int num, struct customer cdata[]){
int count=0, i;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
count++;}
}
return count;
}

float moneyOutstanding(int num, struct customer cdata[]){
int i, sum=0;
for (i=0; i<num; i++){
if (cdata[i].paid=='N'){
sum= sum+(cdata[i].cost*cdata[i].boxes);}
}
return sum;
}

int moneyReceivedPercent(int num, struct customer cdata[]){
int answ;
float money, moneyOut;
money = moneyReceived(num, cdata);
moneyOut = moneyOutstanding(num, cdata);

answ=(moneyOut/money)*100;

return answ;
}

最佳答案

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. C11 §7.21.6.2 8

1) 在"%c"前添加一个空格以消耗空白。 "%c" 本身不会执行此操作。即使没有前导空格,其他使用的说明符也会执行此操作。

2) 检查*scanf()函数的返回值。除非返回值为 6,否则在这种情况下,不要相信扫描成功完成。

3) 通过声明宽度比 '%s' 的大小小一来限制字符串输入。当然应该防止代码超出数组。

for (i=0; i<numCustomers; i++){
// fscanf(file, "%s%s%s%d%f%c", &cdata[i].first, &cdata[i].last,
// &cdata[i].type, &cdata[i].boxes, &cdata[i].cost, &cdata[i].paid);

if (6 != fscanf(file, "%11s%11s%9s%d%f %c",
&cdata[i].first, &cdata[i].last, &cdata[i].type,
&cdata[i].boxes, &cdata[i].cost, &cdata[i].paid)) {
printf("Bad input!");
}
}

关于c - 从文件读取变量并将它们存储在结构中 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34054216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com