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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 3
Reputation: TooMpa is an unknown quantity at this point 
Solved Threads: 1
TooMpa TooMpa is offline Offline
Newbie Poster

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

 
0
  #1
Sep 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #2
Sep 12th, 2008
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC