2015-12-02 108 views
0

我在這個程序中遇到了一些嚴重的麻煩。 我需要從一個文件中讀取數據,這個文件有一組名和姓,一個cookie類型,購買金額,成本以及是否支付。從文件讀取變量並將它們存儲在結構中 - C

我相當有信心,我的功能是正確的。

當我運行該程序時,它運行正常,但在某些數字之間看起來會有額外的數字。我的意思是,如果輸入的文件是: 約翰綠薄荷1 1.00是 簡藍籌2 2.00ñ 輸出文件將是這樣的: 約翰·格林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; 
} 
+0

'fscanf(file,「%s%s%s%d%f%c」,' - >'fscanf(file,「%s%s%s%d%f%c」, '在%c之前添加空格 – chux

+0

上帝的甜媽你真是太好了謝謝你!! – AwesomeSauce

+0

這是爲什麼呢?btw – AwesomeSauce

回答

1

輸入的空白​​字符(由isspace爲函數指定)被跳過,除非本說明書包括一個[c,或n說明符。 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!"); 
    } 
} 
相關問題