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

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

- 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?

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).

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?

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

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

The node containing garbage value appears at the last..

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.

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