2016-11-04 137 views
0

我能夠讓程序打開該文件,但我無法讀取該信息。它需要逐行運行數據,但它一直被我的「錯誤讀取數據」循環所捕獲。我怎樣才能解決這個問題?將.txt文件讀入C程序

#include <stdio.h> 
#include <stdlib.h> 

int getdata(FILE *data, int *empid, int *dept, float *paygr, int *exe, int *hours); 
void calcdata(float paygr, int exe, int hours, float *pay, float *overpay, float *tpay); 
void printdata(int empid, int dept, float paygr, int exe, int hours, float pay, float overpay, float tpay); 

int main(void) 
{ 
    FILE *data; 
    int empid; 
    int dept; 
    float paygr; 
    int exe; 
    int hours; 
    float pay; 
    float overpay; 
    float tpay; 

    data = fopen("emp_data.txt", "r"); 

    if (data == NULL) 
    { 
     printf("Could not open input file\a\n"); 
     exit(101); 
    } 

    while (getdata(data, &empid, &dept, &paygr, &exe, &hours)) 
    { 
     calcdata(paygr, exe, hours, &pay, &overpay, &tpay); 
     printdata(empid, dept, paygr, exe, hours, pay, overpay, tpay); 
    } 

    fclose(data); 

    return 0; 
} 

int getdata(FILE *data, int* empid, int* dept, float *paygr, int *exe, int *hours) 
{ 
    int ioResult; 

    ioResult = fscanf(data, "%d%d%f%d%d", empid, dept, paygr, exe, hours); 
    if (ioResult == EOF) 
    { 
     return 0; 
    } 
    else if (ioResult != 5) 
    { 
     printf("\aError reading data\n"); 
     return 0; 
    } 
    else 
    { 
     return 1; 
    } 
} 

void calcdata(float paygr, int exe, int hours, float *pay, float *overpay, float *tpay) 
{ 
    int overhrs; 

    if (exe == 'N' && hours > 40) 
    { 
     overhrs = hours - 40; 
     *tpay = (paygr * (hours - overhrs)) + (overhrs * (paygr * 1.5)); 
     *pay = (hours - overhrs) * paygr; 
     *overpay = (overhrs * (paygr * 1.5)); 
    } 
    else 
    { 
     *tpay = hours * paygr; 
     *pay = hours * paygr; 
     *overpay = 0.0; 
    } 
} 

void printdata(int empid, int dept, float paygr, int exe, int hours, float pay, float overpay, float tpay) 
{ 
    printf("%d %d %2f %d %d %2f %2f %2f\n", empid, dept, paygr, exe, hours, pay, overpay, tpay); 
} 

下面是emp_data.txt

205  14 7.54 N 45 

3498 64 17.98 Y 38 

1328 38 25.75 Y 49 

9054 76 10.43 N 42 

213  64 8.29 N 47 

659  14 7.52 N 41 

5487 14 25.75 Y 52 

4326 38 7.54 N 36 

1267 76 8.29 N 40 
+2

@LPs:可能帶有嵌入空格;-) – chqrlie

+0

顯示emp_data.txt文件。 – BLUEPIXY

+1

'scanf'是錯誤的。你有指針傳遞函數:'fscanf(data,「%d%d%f%d%d」,&empid,&dept,&paygr,&exe,&hours);' - >'fscanf(data,「%d% d'f%d%d「,empid,dept,paygr,exe,小時);' – LPs

回答

1

這裏是你的代碼的一些問題文本:

  • 的變量應在main函數的局部。

  • 測試!datadata == NULL是多餘的:兩個表達式都是等價的。

  • 員工狀態是一個字符,它應該有一個%c格式解析成char變量

  • 你應該值傳遞給printf,而不是地址。

  • 你也應該通過數據printdata數據太傳遞地址是不需要的。

  • calcpay中的輸入參數應該按值傳遞。只有輸出變量需要通過地址傳遞。

  • 如果沒有超時時間,則必須將overpay設置爲0.0,否則將在未初始化或保留之前員工的值。

  • 打印浮動值代表金錢應該使用2位小數,而不是默認值6.由於缺少類型的精度,您可能會以123456.799999而不是123456.80結束。

  • 換行符表示爲\n,不/n

  • fscanf()讀值時,忽略空格,但在打印時你應該如何分開的值。

  • 轉換說明符的數量與printf的參數數量不匹配。編譯啓用警告,讓編譯器檢測這些愚蠢的錯誤。

這裏是如何糾正代碼:

#include <stdio.h> 
#include <stdlib.h> 

int getdata(FILE *data, int *empid, int *dept, float *paygr, int *exe, int *hours); 
void calcdata(float paygr, int exe, int hours, float *pay, float *overpay, float *tpay); 
void printdata(int empid, int dept, float paygr, int exe, int hours, float pay, float overpay, float tpay); 

int main(void) { 
    FILE *data; 
    int empid; 
    int dept; 
    float paygr; 
    char exe; 
    int hours; 
    float pay; 
    float overpay; 
    float tpay; 

    data = fopen("emp_data.txt", "r"); 

    if (data == NULL) { 
     printf("Could not open input file\a\n"); 
     exit(101);  
    } 

    while (getdata(data, &empid, &dept, &paygr, &exe, &hours)) { 
     calcdata(paygr, exe, hours, &pay, &overpay, &tpay); 
     printdata(empid, dept, paygr, exe, hours, pay, overpay, tpay); 
    } 

    fclose(data); 

    return 0; 
} 

int getdata(FILE *data, int* empid, int* dept, float *paygr, char *exe, int *hours) { 
    int ioResult; 

    ioResult = fscanf(data, "%d%d%f %c%d", &empid, &dept, &paygr, &exe, &hours); 
    if (ioResult == EOF) { 
     return 0; 
    } else 
    if (ioResult != 5) { 
     printf("\aError reading data\n"); 
     return 0; 
    } else { 
     return 1; 
    } 
} 

void calcdata(float paygr, char exe, int hours, float *pay, float *overpay, float *tpay) { 
    int overhrs; 

    if (exe == 'N' || hours > 40) { 
     overhrs = hours - 40; 
     *tpay = (paygr * (hours - overhrs)) + (overhrs * (paygr * 1.5)); 
     *pay = (hours - overhrs) * paygr; 
     *overpay = (overhrs * (paygr * 1.5)); 
    } else { 
     *tpay = hours * paygr; 
     *pay = hours * paygr; 
     *overpay = 0.0; 
    } 
} 

void printdata(int empid, int dept, float paygr, char exe, int hours, 
       float pay, float overpay, float tpay) { 
    printf("%d %d %2f %c %d %2f %2f %2f\n", 
      empid, dept, paygr, exe, hours, pay, overpay, tpay); 
} 
+1

@LPs:很好!添加和修復。 – chqrlie

+0

謝謝,這有很大的幫助。不過,我仍然遇到和以前一樣的問題,它不會從文件中讀取數據。我仍然收到「錯誤讀取數據」消息。 – dazedandconfusedeestudent

+0

@dazedandconfusedeestudent:這個答案解決了你的問題嗎? – chqrlie

0

如果你還有問題讀取文件-supposing你給的的fopen(...)正確的文件路徑,請嘗試以下完成代碼

#include <stdio.h> 
#include <stdlib.h> 


    int getdata(FILE *data, int *empid, int *dept, float *paygr, char 
*exer,int *hours); 



    void calcdata(float paygr, char *exer, int hours, float *pay, float 
    *overpay, float *tpay); 

    void printdata(int empid, int dept, float paygr, char *exer, int hours, 
    float pay, float overpay, float tpay); 

    int main(void) { 
    FILE *data; 
    int empid; 
    int dept; 
    float paygr; 
    char exea[2]; 
    int hours; 
    float pay; 
    float overpay; 
    float tpay; 
    int ioresult; 
    data = fopen("emp_data.txt", "r"); 

    if (data == NULL) { 
    printf("Could not open input file\a\n"); 
    exit(101);  
    } 

    while (getdata(data, &empid, &dept, &paygr, &exea[0], &hours)) { 
    calcdata(paygr, &exea[0], hours, &pay, &overpay, &tpay); 
    printdata(empid, dept, paygr, &exea[0], hours, pay, overpay, tpay); 
    } 
    fclose(data); 

    return 0; 
} 


int getdata(FILE *data, int* empid, int* dept, float *paygr, char *exer, 
int *hours) { 
    int ioResult; 
    ioResult = fscanf(data,"%d%d%f%s%d",empid,dept,paygr,exer,hours); 
    if (ioResult == EOF) { 
     return 0; 
    } else 
    if (ioResult != 5) { 
     printf("\aError reading data\n"); 
     return 0; 
    } else { 
     return 1; 
    } 
    } 

    void calcdata(float paygr, char *exer, int hours, float *pay, float 
    *overpay, float *tpay) { 
    int overhrs; 

    if (exer[0] == 'N' || hours > 40) { 
    overhrs = hours - 40; 
    *tpay = (paygr * (hours - overhrs)) + (overhrs * (paygr * 1.5)); 
    *pay = (hours - overhrs) * paygr; 
    *overpay = (overhrs * (paygr * 1.5)); 
    } else { 
      *tpay = hours * paygr; 
      *pay = hours * paygr; 
      *overpay = 0.0; 
      } 
    } 

    void printdata(int empid, int dept, float paygr, char *exer, int hours, 
      float pay, float overpay, float tpay) { 
    printf("%d %d %f %s %d %f %f %f\n",empid, dept, paygr, exer, 
    hours,pay,overpay,tpay); 
    } 

這將工作。