arrays - Read from a file and tokenise input C -
i'm having trouble piece of coding i'm working on. involves linked lists , annoying pointers. here sample code:
persontype *person; file *c; c = fopen("file.csv", "r"); char in[100]; fgets(in, 80, c); //edited root->head->next = 0; char *getnum = strtok(in, ";"); char *getname = strtok(null, ";"); char *gethome = strtok(null, ";"); strcpy(root->head->getnum, getnum); strcpy(root->head->getname, getname); strcpy(root->head->gethome, gethome); person = root->head; if(person != 0){ while(person->next != 0){ person=person->next; } } //degug printf("successfully made person node"); the code won't read file , stumped why. in xcode debugger/tracer initializes chars nil. can please point me in right direction?
thanks.
example file
p1;elyza;45 random rd p2;ian;78 shark cl
c = fopen("file.csv", "r"); char in[100]; fgets(in, 80, cin); you meant
c = fopen("file.csv", "r"); char in[100]; fgets(in, 80, c); // <--- also make sure check return value fopen() handle case file not found.
Comments
Post a Comment