943,918 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 583
  • C RSS
Sep 11th, 2008
0

Sort a inputfile to linked list and print it, some problems.. PLZ help :)

Expand Post »
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>.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main(int argc, char *argv[]){
  5.  
  6. FILE *file;
  7. char line [ 128 ], x;
  8. int y;
  9. unsigned int tmpUid;
  10. void* tmpName;
  11.  
  12. file=(fopen(argv[1],"r"));
  13.  
  14. printf("make node and stortNode");
  15.  
  16. struct node {
  17. unsigned int uid;
  18. char* name;
  19. struct node* next;
  20. } user, *p, sortUser, *sp;
  21.  
  22.  
  23. printf("using malloc for nodes");
  24.  
  25. p = malloc(sizeof(struct node));
  26. sp = malloc(sizeof(struct node));
  27.  
  28. printf("checking if file is empty");
  29.  
  30. if(file != NULL){
  31.  
  32. printf("starting the read loop");
  33.  
  34. while( fgets ( line, 128, file ) != NULL ){
  35. printf("first-loop");
  36. char str[10];
  37. y=0;
  38.  
  39. while(x!='\n'){
  40.  
  41. printf("second-loop");
  42.  
  43. int i;
  44. int size;
  45.  
  46. x=fgetc(file);
  47.  
  48. if(x==':')
  49. y++;
  50.  
  51. if(y=0){
  52. size++;
  53. realloc(user.name,(sizeof(struct node)) + size);
  54. user.name=&x;
  55. }
  56.  
  57. if(y=2){
  58. x=str[i];
  59. i++;
  60. }
  61. }
  62.  
  63. user.uid=atoi(str);
  64. p=p->next;
  65. p->next = NULL;
  66. }
  67.  
  68. while(sp!=NULL){
  69. printf("trying to sort!");
  70. if(sp->uid < p->uid){
  71.  
  72. sp->uid = p->uid;
  73. sp++;
  74. }
  75. p++;
  76. }
  77.  
  78. }
  79.  
  80. free(p);
  81. free(sp);
  82.  
  83. return 0;
  84. }
Last edited by TooMpa; Sep 11th, 2008 at 5:41 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
TooMpa is offline Offline
7 posts
since Sep 2008
Sep 12th, 2008
0

Re: Sort a inputfile to linked list and print it, some problems.. PLZ help :)

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: counting vowels in string
Next Thread in C Forum Timeline: Problems with 'system' call





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC