2014-07-24 3 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

영업 이익의 문제는 같은 줄에 이름과 성을 갖는 " %[^\n]\n %[^\n]\n" 구문 분석 시작했다.

또한 문제 scanf() 가족이 어떤 형식 문자열 공백 지침을 동일하게 취급한다. 형식 문자열의 "\n "" "과 같으며 "\n"입니다. 모두 흰색 공백을 사용합니다.

로서 상자성 @The 크로 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; 
} 
관련 문제