954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading link list in files..

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:

for(p = A; flag != 1;)
	 {
		  *p = (LIST) malloc(sizeof(celltype));
		  if((fread(*p, sizeof(celltype), 1, fp)) == 0)
				flag = 1;
		  p = &(*p)->next;
	 }
	 *p = NULL;

please help

Whilliam
Junior Poster
112 posts since Oct 2008
Reputation Points: 19
Solved Threads: 0
 

- read into temporary memory
- if successful, allocate a node, copy the data and append to the list.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
- 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?

Whilliam
Junior Poster
112 posts since Oct 2008
Reputation Points: 19
Solved Threads: 0
 
LIST *list = NULL;
while ( fread( &temp, sizeof temp, 1, fp ) == 1 ) {
  LIST *node = makeNewNode( &temp ); // allocate and copy
  list = append( list, node ); // append
}

Where is the junk node?
If the file is empty, then the list at the end is NULL (an empty list).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
LIST *list = NULL;
while ( fread( &temp, sizeof temp, 1, fp ) == 1 ) {
  LIST *node = makeNewNode( &temp ); // allocate and copy
  list = append( list, node ); // append
}

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:

for(p = L; p != NULL; p=p->next)
	fwrite(p, sizeof(celltype), 1, fp);

am I doing this right?

Whilliam
Junior Poster
112 posts since Oct 2008
Reputation Points: 19
Solved Threads: 0
 

> p = A;
It seems you start off with a junk node to begin with.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
> p = A; It seems you start off with a junk node to begin with.

The node containing garbage value appears at the last..

Whilliam
Junior Poster
112 posts since Oct 2008
Reputation Points: 19
Solved Threads: 0
 
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 :

for(p = L; p->next != NULL; p=p->next)

So that you stop writing just before the last node.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 
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

rampurhaat
Newbie Poster
2 posts since Jul 2009
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You