#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에서 토큰으로 가져온 문자열을 분리합니다.
댓글이 확장 된 논의하지 않습니다; 이 대화는 [채팅으로 이동되었습니다] (http://chat.stackoverflow.com/rooms/144779/discussion-on-question-by-yunus-gedik-im-struggling-with-the-fgets-function-in) . –