943,542 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 736
  • C RSS
Jul 4th, 2009
0

Reading link list in files..

Expand Post »
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
Similar Threads
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 4th, 2009
0

Re: Reading link list in files..

- read into temporary memory
- if successful, allocate a node, copy the data and append to the list.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 4th, 2009
0

Re: Reading link list in files..

Click to Expand / Collapse  Quote originally posted by Salem ...
- 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?
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 4th, 2009
0

Re: Reading link list in files..

  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).
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 4th, 2009
0

Re: Reading link list in files..

Click to Expand / Collapse  Quote originally posted by Salem ...
  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?
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 4th, 2009
0

Re: Reading link list in files..

> p = A;
It seems you start off with a junk node to begin with.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 4th, 2009
0

Re: Reading link list in files..

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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.
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 5th, 2009
0

Re: Reading link list in files..

Click to Expand / Collapse  Quote originally posted by Whilliam ...
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 5th, 2009
0

Re: Reading link list in files..

Click to Expand / Collapse  Quote originally posted by Whilliam ...
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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
rampurhaat is offline Offline
2 posts
since Jul 2009

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: Program Keeps reading wrong input
Next Thread in C Forum Timeline: The C Programming Language





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


Follow us on Twitter


© 2011 DaniWeb® LLC