| | |
Sort a inputfile to linked list and print it, some problems.. PLZ help :)
![]() |
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 1
Hi have a problem, this code should read a file and then save it in a linked list, just the <uid><name> from a lie looking like: <username>:<password>:<uid>:<gid>:<fullname>:<home dir>:<shell>
I can't find the segmentation fault error atm, but hints on the code would be helpfull.
Atfer the insert in user list the info should be sorted to the sortUser, with the smalest int first <uid>.
I can't find the segmentation fault error atm, but hints on the code would be helpfull.
Atfer the insert in user list the info should be sorted to the sortUser, with the smalest int first <uid>.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[]){ FILE *file; char line [ 128 ], x; int y; unsigned int tmpUid; void* tmpName; file=(fopen(argv[1],"r")); printf("make node and stortNode"); struct node { unsigned int uid; char* name; struct node* next; } user, *p, sortUser, *sp; printf("using malloc for nodes"); p = malloc(sizeof(struct node)); sp = malloc(sizeof(struct node)); printf("checking if file is empty"); if(file != NULL){ printf("starting the read loop"); while( fgets ( line, 128, file ) != NULL ){ printf("first-loop"); char str[10]; y=0; while(x!='\n'){ printf("second-loop"); int i; int size; x=fgetc(file); if(x==':') y++; if(y=0){ size++; realloc(user.name,(sizeof(struct node)) + size); user.name=&x; } if(y=2){ x=str[i]; i++; } } user.uid=atoi(str); p=p->next; p->next = NULL; } while(sp!=NULL){ printf("trying to sort!"); if(sp->uid < p->uid){ sp->uid = p->uid; sp++; } p++; } } free(p); free(sp); return 0; }
Last edited by TooMpa; Sep 11th, 2008 at 4:41 am.
It seems you don't understand basics of dynamic memory allocation:
1. struct node has char*name member (pointer to char) but you don't allocate memory for name object. So you have garbage pointer values in uninitialized structures. Use malloc to allocate memory and save pointers to these structure members for all struc node variables.
2. realloc() returns a NEW pointer for reallocated memory. You must save it then reuse. Now you lost this new pointer and continue to work with invalid (old) pointer value.
3. It seems your list building method is incorrect. Take a pencil and a sheet of paper and try to check the algorithm step by step.
4. Test if (file == NULL) is not "checking if file is empty": fopen returns 0 if it can't open the file! Test file immediately after opening and stop processing if nothing to do.
Resolve #1, #2 and #3 points then come back...
1. struct node has char*name member (pointer to char) but you don't allocate memory for name object. So you have garbage pointer values in uninitialized structures. Use malloc to allocate memory and save pointers to these structure members for all struc node variables.
2. realloc() returns a NEW pointer for reallocated memory. You must save it then reuse. Now you lost this new pointer and continue to work with invalid (old) pointer value.
3. It seems your list building method is incorrect. Take a pencil and a sheet of paper and try to check the algorithm step by step.
4. Test if (file == NULL) is not "checking if file is empty": fopen returns 0 if it can't open the file! Test file immediately after opening and stop processing if nothing to do.
Resolve #1, #2 and #3 points then come back...
![]() |
Other Threads in the C Forum
- Previous Thread: counting vowels in string
- Next Thread: Problems with 'system' call
Views: 468 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
#include api array arrays binary binarysearch bit c++ calling char character code coke command conversion convert database decimal dice directory directorystructure do-not-use drawing dude dynamic error exec executable factorial fgets file fork framework free function functions givemetehcode givemetehcodez grade graphics gtkgcurlcompiling haiku hangman help|help|help|help homework i/o input insert int integer intmain() lazy line linked linkedlist linux list lists loop lowest malloc matrix memory mysql no-effort output parallel path permutations pointer pointers problem process profile program programming read recursion recursive recv reverse scanf segmentationfault socketprograming spoonfeeding stack string strings strtok structures student system testing turbo-c turboc unix user variable windows _getdelim






