File problem in link list

Reply

Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

File problem in link list

 
0
  #1
Jul 2nd, 2009
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..

  1. void save_file(LIST L)
  2. {
  3. FILE *fp;
  4. char filename[30];
  5. LIST p;
  6.  
  7. printf("What is the name of the file? ");
  8. flushall();
  9. gets(filename);
  10.  
  11. if((fp = fopen(filename, "wb")) == NULL)
  12. {
  13. printf("Cannot write file");
  14.  
  15. }
  16. for(p = L; p != NULL; p=p->next)
  17. fwrite(&p, sizeof(celltype), 1, fp);
  18.  
  19. printf("File Saved");
  20. fclose(fp);
  21. getch();
  22. clrscr();
  23. }
  24. void load_file(LIST *A)
  25. {
  26. FILE *fp;
  27. char filename[30];
  28. size_t c;
  29. LIST p, temp;
  30.  
  31. printf("What is the filename? ");
  32. flushall();
  33. gets(filename);
  34. if((fp = fopen(filename, "rb")) ==NULL)
  35. {
  36. printf("File does not exist");
  37.  
  38. }
  39. for(p = *A; p != NULL; p = p->next)
  40. {
  41. fread(&p, sizeof(celltype), 1, fp);
  42. }
  43.  
  44. printf("File Loaded");
  45. getch();
  46. clrscr();
  47. }
Last edited by Whilliam; Jul 2nd, 2009 at 8:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: File problem in link list

 
0
  #2
Jul 2nd, 2009
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
  1. struct node{
  2. char *store_this;
  3. };
And after all the proper memory assignments for the node and the character array you should use it as:
  1. for(p = L; p != NULL; p=p->next)
  2. 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:
  1. typedef struct node LIST;
Then the statements p=p->link; loose value. Its kind of an confusion with your program.
Last edited by csurfer; Jul 2nd, 2009 at 8:44 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: File problem in link list

 
0
  #3
Jul 2nd, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: File problem in link list

 
0
  #4
Jul 2nd, 2009
sob.. its still not working..

here's my main function and structure:

  1. typedef struct
  2. {
  3. char LN[16];
  4. char FN[24];
  5. char MI;
  6. }name;
  7.  
  8. typedef struct
  9. {
  10. unsigned long ID;
  11. name n;
  12. char course[8];
  13. int year;
  14. }studrec;
  15.  
  16. typedef struct cell
  17. {
  18. studrec s;
  19. struct cell *next;
  20. }celltype, *LIST
  21.  
  22. void main(void)
  23. {
  24. LIST L, A;
  25. char ans;
  26. int quit = 0;
  27. studrec n;
  28.  
  29. L = NULL;
  30. A = NULL;
  31.  
  32. clrscr();
  33.  
  34. do
  35. {
  36. printf("Press\nN for Input\nI for Insert\nS to save file\nL to load a file\nV to view\nQ to quit\n");
  37. ans = getch();
  38. clrscr();
  39.  
  40. if(ans == 'N' || ans == 'n')
  41. {
  42. input(&n);
  43. clrscr();
  44. }
  45. else if(ans == 'I' || ans == 'i')
  46. insert(&L, n);
  47. else if(ans == 'S' || ans == 's')
  48. save_file(L);
  49. else if(ans == 'L' || ans == 'l')
  50. {
  51. load_file(&A);
  52. view(A);
  53. }
  54. else if(ans == 'V' || ans == 'v')
  55. {
  56. view(L);
  57. getch();
  58. clrscr();
  59. }
  60. else if(ans == 'Q' || ans == 'q')
  61. quit = 1;
  62. }while(quit == 0);
  63. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,434
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 439
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: File problem in link list

 
0
  #5
Jul 3rd, 2009
How many records in a data file?

  1. for(p = *A; p != NULL; p = p->next)
  2. {
  3. fread(&p, sizeof(celltype), 1, fp);
  4. }
You have to test the eof().

Can fread() allocate a node?
?
Last edited by adatapost; Jul 3rd, 2009 at 1:12 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: File problem in link list

 
0
  #6
Jul 3rd, 2009
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!!!
Last edited by csurfer; Jul 3rd, 2009 at 1:30 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC