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

时间:2015-12-02 22:19:44

标签: c struct io

我在使用这个程序时遇到了一些麻烦。 几乎我需要从一个包含名字和姓名,cookie类型,购买金额,成本以及是否付款的文件中读取。

我相信我的功能是正确的。

当我运行程序时,它运行正常,但在某些数字之间看起来是额外的数字。我的意思是,如果输入文件是: 约翰格林薄荷1 1.00 Y. Jane Blue筹码2 2.00 N. 输出文件将类似于: John Green 12 20000 mint 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;
}

1 个答案:

答案 0 :(得分:1)

  

除非规范包含[cn说明符,否则将跳过输入空白字符(由isspace函数指定)。 C11§7.21.6.28

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!");
  }
}