Reading link list in files..

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

Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Reading link list in files..

 
0
  #1
Jul 4th, 2009
Hello.. How can I read a link list from a file without reading a garbage node? I don't know much about how file works..
This is my code fragment:
  1. for(p = A; flag != 1;)
  2. {
  3. *p = (LIST) malloc(sizeof(celltype));
  4. if((fread(*p, sizeof(celltype), 1, fp)) == 0)
  5. flag = 1;
  6. p = &(*p)->next;
  7. }
  8. *p = NULL;
please help
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading link list in files..

 
0
  #2
Jul 4th, 2009
- read into temporary memory
- if successful, allocate a node, copy the data and append to the list.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: Reading link list in files..

 
0
  #3
Jul 4th, 2009
Originally Posted by Salem View Post
- read into temporary memory
- if successful, allocate a node, copy the data and append to the list.
I don't think that will ignore the garbage node..
should I just put a sentinel value in my structure when I wright files instead?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading link list in files..

 
0
  #4
Jul 4th, 2009
  1. LIST *list = NULL;
  2. while ( fread( &temp, sizeof temp, 1, fp ) == 1 ) {
  3. LIST *node = makeNewNode( &temp ); // allocate and copy
  4. list = append( list, node ); // append
  5. }
Where is the junk node?
If the file is empty, then the list at the end is NULL (an empty list).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: Reading link list in files..

 
0
  #5
Jul 4th, 2009
Originally Posted by Salem View Post
  1. LIST *list = NULL;
  2. while ( fread( &temp, sizeof temp, 1, fp ) == 1 ) {
  3. LIST *node = makeNewNode( &temp ); // allocate and copy
  4. list = append( list, node ); // append
  5. }
Where is the junk node?
If the file is empty, then the list at the end is NULL (an empty list).
it reads an extra loop which contains garbage values..

this is my "wright" code fragment:
  1. for(p = L; p != NULL; p=p->next)
  2. fwrite(p, sizeof(celltype), 1, fp);
am I doing this right?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading link list in files..

 
0
  #6
Jul 4th, 2009
> p = A;
It seems you start off with a junk node to begin with.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: Reading link list in files..

 
0
  #7
Jul 4th, 2009
Originally Posted by Salem View Post
> p = A;
It seems you start off with a junk node to begin with.
The node containing garbage value appears at the last..
Last edited by Whilliam; Jul 4th, 2009 at 8:54 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Reading link list in files..

 
0
  #8
Jul 5th, 2009
Originally Posted by Whilliam View Post
The node containing garbage value appears at the last..
So if your concern is just the last but one node that is if you say that the node whose node->link is your junk node and you don't want to read it then you can just do this :
  1. for(p = L; p->next != NULL; p=p->next)
So that you stop writing just before the last node.
Last edited by csurfer; Jul 5th, 2009 at 12:04 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: rampurhaat is an unknown quantity at this point 
Solved Threads: 0
rampurhaat rampurhaat is offline Offline
Newbie Poster

Re: Reading link list in files..

 
0
  #9
Jul 5th, 2009
Originally Posted by Whilliam View Post
The node containing garbage value appears at the last..
It would appear at the last, in case the way you append is something that you have written to actually add the node to the beginning rather than the end. Just make sure that is not the case.

SNIP
Last edited by happygeek; Jul 6th, 2009 at 10:18 am. Reason: fake sig snipped, please read the rules regarding signatures
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC