Hi! Im a newbie here....

I got a problem with my program...My linked list is ok but everytime I export the data to the file, things don't work in a proper way..I will show my code...pls help me...thanks...

labname[20]="lab445.txt";
/****************FWRITE FUNCTION**********************/

void save_data(information *lab,char labname[])
{
   FILE *fp;
   int num;
   char temp[15];

   strcpy(temp,labname);
   temp[6]='\0';
  /* if(lab==NULL)
	{ clrscr();
	  logo();
	  gotoxy(29,10);printf("%s has an Empty List",temp);
	  gotoxy(27,14);printf("Press any key to continue..");
	} */
 
	 if((fp=fopen(labname,"wt"))==NULL)
	    { clrscr();
	      logo();
	      gotoxy(38,10);printf("Error!");
	    }
	  else
	    { 
		  if(lab==NULL)
		     putc('\0',fp);
		  else
		   {	num=0;
			   while(lab!=NULL)
				   { fwrite(lab,sizeof(information),1,fp);
					 num++;
					 lab=lab->next;
				   }
				clrscr();
				printf("There are %d copied from the list.",num);
				getch();
				clrscr();
				logo();

			  for(num=0;num<12;num++)
				  { gotoxy(29,10);printf("Saving Data from %s.",temp);
					delay(40000);
				  }
				clrscr();
				logo();
				gotoxy(36,12);printf("Success!!");
				gotoxy(27,14);printf("Press any key to continue..");
			}		
		  fclose(fp);
		}
	
   getch();
}

/********FOR FREAD FUNCTION*****************/

void ret_data(information **lab,char labname[])
{
    information *temp;
    FILE *fp;
    int x=0;
    char namelab[20];

      clrscr();
    if((fp=fopen(labname,"rt"))==NULL)
       { clrscr();
		 logo();
		 strcpy(namelab,labname);
		 namelab[6]='\0';
		 gotoxy(28,10);printf("NO FILE FOUND in %s",namelab);
		 gotoxy(28,14);printf("Press any key to continue..");
		 getch();
       }
    else
      {  while(!feof(fp))
			{
			   temp=(information*)malloc(sizeof(information));

			   if((fread(temp,sizeof(information),1,fp))!=0)
                   {
					 while(*lab!=NULL)
						lab=&(*lab)->next;

						temp->next=*lab;
						*lab=temp;

					x++;
				   }
			}
		  fclose(fp);
	    clrscr();
	    printf("There are %d files copied from the file.",x);
	    getch();
	  }


}

I would have guessed that "things don't work in a proper way". Way too vague, however. :(

First, post your code with CODE TAGS around it, so it doesn't look like what the dog threw up last week, and second, post the errors - the SPECIFIC ERRORS, that you are experiencing with the program.

Respect the fact that people are giving you free advice, and don't waste their time being coy about the details. Code lives or dies based on the details.

Try again, please! ;)

>>lab=&(*lab)->next;

That is destroying the original pointer. Use a different pointer to iterate through the linked list and leave the original pointer unchanged.

Something like the code below (not compiled or tested). You didn't post the definition of struct information, so I'll just assume it contains no other pointers than the pointer to the next structure.

information* node = NULL;
information* tail = NULL;
information temp; // note that this is NOT a pointer!
while( fread(&temp,sizeof(information),1,fp))>0) )
{
  node = malloc( sizeof(information) );
  memcpy(node,&temp, sizeof(information));
  node->next = NULL;
  if( *lab == NULL)
  {
     *lab = node;
     tail = node;
  }
  else
  {
     tail->next = node;
     tail = node;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.