2014-07-24 52 views
-1

正在將文件中的信息掃描到結構中,然後顯示以檢查它是否正確輸入。將數據從文件掃描到結構

我使用開發的C++中C.

編寫出於某種原因,該信息不被正確掃描,並且不顯示在所有。 任何幫助,將不勝感激。從記事本

23 
kk 
f l 
23 kk 
50000 
shfbskfjabdsbf 

45 
jj 
b l 
45 yy 
80000 
gdfygfyfhgu 

NOVA.txt的

#include <stdio.h> 
#include <string.h> 
typedef struct 
{ 
    int client_id; 
    char client_business_name [30]; 
    char client_first_name [20]; 
    char client_last_name [20]; 
    char client_address [40]; 
    float client_budget; 
    char client_business_info [300]; 
}Client; 
main() 
{ 
    Client c[100]; 
    int x; 
    FILE*z=fopen("NOVA.txt","r"); 

    for (x=0;x<100;x++) 
    { 
     c[x].client_id=-1; 
     strcpy(c[x].client_business_name,"NULL"); 
     strcpy(c[x].client_first_name,"NULL"); 
     strcpy(c[x].client_last_name,"NULL"); 
     strcpy(c[x].client_address,"NULL"); 
     c[x].client_budget=-1; 
     strcpy(c[x].client_business_info,"NULL"); 
    } 

    for (x=0;x<100;x++) 
    { 
     fscanf (z,"%d\n %[^\n]\n %[^\n]\n %[^\n]\n %[^\n]\n%f\n %[^\n]\n\n", 
       &c[x].client_id, c[x].client_business_name, c[x].client_first_name, 
       c[x].client_last_name, c[x].client_address, &c[x].client_budget, 
       c[x].client_business_info); 
    } 

    for (x=0;x<100;x++) 
    { 
     printf("\n%d\n",c[x].client_id); 
     printf("%s\n",c[x].client_business_name); 
     printf("%s\n",c[x].client_first_name); 
     printf("%s\n",c[x].client_last_name); 
     printf("%s\n",c[x].client_address); 
     printf("%f\n",c[x].client_budget); 
     printf("%s\n",c[x].client_business_info); 
    } 

    fclose (z); 
    system ("PAUSE"); 
} 

樣品另外,有人可以請張貼固定的代碼是什麼樣子?

+2

給出輸入文件的示例! – Sathish

+1

您應該檢查'fscanf'的返回值以確保所有預期的數據都被正確讀取。 –

+2

您不僅應該檢查'fscanf()'的值,還應該在使用之前檢查'fopen()'的值。對掃描的琴絃長度應用限制會更好。 –

回答

0

OP的問題始於在同一行上的名字和姓氏,並使用" %[^\n]\n %[^\n]\n"進行解析。

由於scanf()家族將任何格式字符串空白指令視爲相同,所以存在更多問題。 "\n "的格式字符串與" ""\n"相同。所有的意思是消耗任何空白。

作爲@順磁羊角麪包指出,使用fscanf()有處理意外輸入的麻煩。以下是使用fgets()並解析讀取字符串的解決方案。額外的錯誤檢查可能包括在float和姓/名行末尾尋找額外數據。也可以使用strtol()strtof()這可以使用更好的錯誤檢查。請注意使用寬度受限的格式(如"%299[^\n]")來防止緩衝區溢出。始終檢查scanf()函數的返回值。

建議在單獨的函數中解析複雜記錄。

int parse_Client(FILE *z, Client *record) { 
    // In case something fails, at least zero the record 
    memset(record, 0, sizeof *record); 
#if 0 
    // Could use separate buffers scaled to eash reasonable line size. 
    char line0[sizeof record.client_id * CHAR_BIT/3 + 3 + 1]; 
    char line1[sizeof record.client_business_name + 1]; 
    char line2[sizeof record.client_first_name + 1 + sizeof record.client_last_name + 1]; 
    char line3[sizeof record.client_address + 1]; 
    char line4[1 + 1 + FLT_DECIMAL_DIG + 1 + 1 + 5 + 2]; 
    char line5[sizeof record.client_business_info + 1]; 
#else 
    // But instead, use the sizeof Client to get a reasonable working buffer. 
    char line[sizeof record]; 
#endif 
    for (int i = 0; i < 6; i++) { 
    if (fgets(line, sizeof line, z) == NULL) 
     return FAIL; 
    switch (i) { 
     case 0: 
     if (sscanf(line, "%d", &record->client_id) != 1) 
      return FAIL; 
     break; 
     case 1: 
     if (sscanf(line, " %29[^\n]", record->client_business_name) != 1) 
      return FAIL; 
     break; 
     case 2: 
     if (sscanf(line, "%19s%19s", record->client_first_name, 
       record->client_last_name) != 2) 
      return FAIL; 
     break; 
     case 3: 
     if (sscanf(line, "%39[^\n]", record->client_address) != 1) 
      return FAIL; 
     break; 
     case 4: 
     if (sscanf(line, "%f", &record->client_budget) != 1) 
      return FAIL; 
     break; 
     case 5: 
     if (sscanf(line, "%299[^\n]", record->client_business_info) != 1) 
      return FAIL; 
     break; 
    } 
    } 
    return 0; 
}