2017-05-20 1 views
1
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

#define NAMELEN 50 
#define MAXRECORD 500 

typedef struct record_s { 
    int id; 
    char number[NAMELEN]; 
    struct record_s *next; 
} Record; 

typedef struct person_s { 
    int id; 
    char name[NAMELEN]; 
    double expenditure; 
    Record *numbers; 
} Person; 

typedef struct people_s { 
    Person data[MAXRECORD]; 
    int size; 
} People; 

void read(char* filename, People *people) { 
    FILE* fp=fopen(filename,"r"); 
    int i=0; //data[i] 
    char ilk[NAMELEN]=""; //Name 
    char son[NAMELEN]=""; //Surname 
    char str[256]=""; //To strtok function. 
    char *token; //To strtok function. 

    people->size=0; 
    people->data[i].numbers=malloc(sizeof(struct record_s)); 
    people->data[i].numbers->next=NULL; 

    while(fgets(str,256,fp)!=NULL){ 
     token=strtok(str," "); 
     people->data[i].id=atoi(token); 
     token=strtok(str," "); 
     strcpy(ilk,token); 
     token=strtok(str," "); 
     strcpy(son,token); 
     token=strtok(str," "); 
     people->data[i].expenditure=atof(token); 
     strcat(ilk," "); 
     strcat(ilk,son); 
     strcpy(people->data[i].name,ilk); 

     token=strtok(str," "); 
      people->data[i].numbers->id=people->data[i].id; 
     strcpy(people->data[i].numbers->number,token); 
     token=strtok(str," "); 

     while(token!=NULL){ 
      people->data[i].numbers->next=malloc(sizeof(struct record_s)); 
      people->data[i].numbers->next->next=NULL; 
      people->data[i].numbers=people->data[i].numbers->next; 

      people->data[i].numbers->id=people->data[i].id; 
      strcpy(people->data[i].numbers->number,token); 
      token=strtok(str," "); 

     } 

     i++; 
     (people->size)++; 
    } 

    fclose(fp); 
} 

void print(People people) { 
    int i,found = 0; 
    Record *rec; 
    /* header */ 
    printf("%-5s %-30s %-20s %-20s\n", "ID","NAME","EXPENDITURE","NUMBER(s)"); 
    /* line */ 
    for (i = 0; i < 78; ++i) 
    printf("-"); 
    printf("\n"); 

    for (i = 0; i < people.size; ++i) { 
     found = 0; 
     printf("%-5d %-30s %-20.4f", people.data[i].id, people.data[i].name,   people.data[i].expenditure); 
     rec = people.data[i].numbers; 
     while(rec) { 
      if(found) 
       printf("%57s", ""); 
      else 
       found = 1; 
      printf("%-20s\n", rec->number); 
      rec = rec->next; 
     } 
     printf("\n"); 
    } 
} 


int main(int argc, char** argv) { 
    People people1,people2; 
    people1.size = 0; 
    read(argv[1],&people1); 
    print(people1); 
    return 0; 
} 

나는 핵심 둠 그래서 gdb를보고 시도 얻을까지이 온다 : 나는 왜 독방 감금 오류를 뻥 아무 생각나는 C에서는 fgets 기능과 사투를 벌인거야

Program received signal SIGSEGV, Segmentation fault. _IO fgets (buf=0x7fffffff 5150 "", n=256, fp=0x0) at iofgets.c:50 50 iofgets.c: No such file or directory. 

그리고 .

내 텍스트 파일 : 나는 텍스트 파일을 읽을 수는 fgets() 함수를 사용

123 Asaf Avidan 25.2 05411975476 02224759866 
221 Goksel Baktagir 12.4 03127586632 
122 Goran Bregovic 10 02167418632 
101 Marjan Farsad 27.8 05558741937 0555279431 
36 Mark Eliyahu 36.6 05412147596 05423214587 05335841937 
27 Mohsen Namjoo -1 05421458698 05447851489 
192 Erkan Ogur 49.5 02162547896 02125847596 05325874586 05335412586 

나는 이름이있는 사람 몰라요 ı 부동 소수점 및 전화 번호를 ID 읽으려고 얼마나 많은 있도록 strtok() 함수를 사용하여 fgets에서 토큰으로 가져온 문자열을 분리합니다.

+0

댓글이 확장 된 논의하지 않습니다; 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/144779/discussion-on-question-by-yunus-gedik-im-struggling-with-the-fgets-function-in) . –

답변

1

fopen()이 실패 할 수 있습니다. 그런 다음 NULL을 반환하고 NULL에서 읽으려는 시도는 fgets()이됩니다.

fgets (buf=0x7fffffff 5150 "", n=256, fp=0x0) 

0가에서 "읽기"로 파일 포인터 값으로 전달되었다는 것을 알려줍니다. NULL은 대부분 0으로 구현됩니다.

실패 할 수있는 모든 기능에 대해 오류 검사를 추가하려고합니다.

이 같은 예를 들어 그렇게 할 수 있습니다 :

FILE* fp=fopen(filename,"r"); 
    if (NULL == fp) 
    { 
    perror("fopen() failed"); 
    exit(EXIT_FAILURE); /* include <stdlib.h> to have the EXIT_* macros available. */ 
    } 
+0

나는 노력했지만 아무 것도 바뀌지 않았다. –

+0

@YunusGedik : GDB에서 똑같은 오류가 발생합니다. – alk

+0

예 정확히 같은 오류 –

관련 문제