hello.. its me again and Im having problems with files again..this time, its with link list..

I can't get my program to display the contents it reads from a file.
Please help..
I want to solve this myself but I have no time, my exam will start in 1 hour..:(

void save_file(LIST L)
{
	 FILE *fp;
	 char filename[30];
	 LIST p;

	 printf("What is the name of the file? ");
	 flushall();
	 gets(filename);

	 if((fp = fopen(filename, "wb")) == NULL)
	 {
	printf("Cannot write file");

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

	 printf("File Saved");
	 fclose(fp);
	 getch();
	 clrscr();
}
void load_file(LIST *A)
{
	 FILE *fp;
	 char filename[30];
	 size_t c;
	 LIST p, temp;

	 printf("What is the filename? ");
	 flushall();
	 gets(filename);
	 if((fp = fopen(filename, "rb")) ==NULL)
	 {
	printf("File does not exist");

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

	 printf("File Loaded");
	 getch();
	 clrscr();
}

Recommended Answers

I hope your implementation of list is something like : typedef struct node * LIST; In the save_file function:

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

You need to specify the address of the character array which holds the value.Say your structure is as

       
Jump to Post

Exactly as csurfer said. You're trying to traverse your linked list but you're also overwriting each node from the file read thus invalidating your pointer!

Exit your functions upon errors. Don't keep trying to load or save data when you have no file handle!

Jump to Post

All 5 Replies

I hope your implementation of list is something like : typedef struct node * LIST; In the save_file function:

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

You need to specify the address of the character array which holds the value.Say your structure is as

struct node{
char *store_this;
};

And after all the proper memory assignments for the node and the character array you should use it as:

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

And the statements LIST *A and p=*A; are absolute nonsence because if p is of type node* then p=A is enough and if p is of type node then p=*A becomes meaningless.

And if you have implemented the list as something like:

typedef struct node LIST;

Then the statements p=p->link; loose value. Its kind of an confusion with your program.

Exactly as csurfer said. You're trying to traverse your linked list but you're also overwriting each node from the file read thus invalidating your pointer!

Exit your functions upon errors. Don't keep trying to load or save data when you have no file handle!

sob.. its still not working..

here's my main function and structure:

typedef struct
{
     char LN[16];
     char FN[24];
     char MI;
}name;

typedef struct
{
     unsigned long ID;
     name n;
     char course[8];
     int year;
}studrec;

typedef struct cell
{
	  studrec s;
	  struct cell *next;
}celltype, *LIST

void main(void)
{
		LIST L, A;
		char ans;
		int quit = 0;
		studrec n;

		L = NULL;
      A = NULL;

		clrscr();

		do
		{
	  printf("Press\nN for Input\nI for Insert\nS to save file\nL to load a file\nV to view\nQ to quit\n");
	  ans = getch();
	  clrscr();

	  if(ans == 'N' || ans == 'n')
	  {
			 input(&n);
			 clrscr();
	  }
	  else if(ans == 'I' || ans == 'i')
			 insert(&L, n);
	  else if(ans == 'S' || ans == 's')
			 save_file(L);
	  else if(ans == 'L' || ans == 'l')
	  {
			 load_file(&A);
			 view(A);
	  }
	  else if(ans == 'V' || ans == 'v')
	  {
			 view(L);
			 getch();
			 clrscr();
	  }
	  else if(ans == 'Q' || ans == 'q')
          quit = 1;
		}while(quit == 0);
}

How many records in a data file?

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

You have to test the eof().

Can fread() allocate a node?
?

Hey you want us to do all the work ??? ;) Try on your own man...Put in some effort...Go on reading each line like a compiler does and ya if you are using Boreland C then may be F7 will help you trace program flow line by line.So try to debug it.

Some of the errors which you can easily find :

1>No semicolon after the struct cell.

2>You have declared A and L as LIST A,L; and LIST is itself struct node * type how can you pass pointer to this pointer...? As in p = *A; @adatapost :

Let him correct these primitive mistakes first adatapost memory allocation and other things are off limits if he cannot trace these simple mistakes.Basically he is not putting in the effort required!!!

Be a part of the DaniWeb community

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