943,831 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 551
  • C RSS
Jul 2nd, 2009
0

File problem in link list

Expand Post »
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.
Similar Threads
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 2nd, 2009
0

Re: File problem in link list

I hope your implementation of list is something like :
typedef struct node * LIST;
In the save_file function:
Quote ...
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 2nd, 2009
0

Re: File problem in link list

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 2nd, 2009
0

Re: File problem in link list

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. }
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Jul 3rd, 2009
0

Re: File problem in link list

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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 3rd, 2009
0

Re: File problem in link list

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: file problem
Next Thread in C Forum Timeline: Using isdigit





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC