file problem

Thread Solved

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

 
0
  #1
Jul 2nd, 2009
Hello.. im having a problem with my code.. when i save a file, then choose another variable and load the file, then try to display it, all that it display are garbage values.. i know the problem is either in function save_file or function load_file.. can anyone please help me..

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4.  
  5. #define max 20
  6.  
  7. typedef struct
  8. {
  9. char LN[16];
  10. char FN[24];
  11. char MI;
  12. }name;
  13.  
  14. typedef struct
  15. {
  16. unsigned long ID;
  17. name n;
  18. char course[8];
  19. int year;
  20. }studrec[max];
  21.  
  22. void save_file(studrec s, int n)
  23. {
  24. FILE *fp;
  25. char filename[30];
  26. int ctr;
  27.  
  28. printf("What is the name of the file? ");
  29. flushall();
  30. gets(filename);
  31.  
  32. if((fp = fopen(filename, "wb")) == 0)
  33. {
  34. printf("Cannot write file");
  35.  
  36. }
  37. for(ctr = 0; ctr < n; ctr++)
  38. fwrite(&(s[ctr]), sizeof(studrec), 1, fp);
  39.  
  40. printf("File Saved");
  41. fclose(fp);
  42. getch();
  43. clrscr();
  44. }
  45.  
  46. void load_file(studrec r)
  47. {
  48. FILE *fp;
  49. int ctr;
  50. char filename[30];
  51.  
  52. printf("What is the filename? ");
  53. flushall();
  54. gets(filename);
  55. if((fp = fopen(filename, "rb")) == 0)
  56. {
  57. printf("File does not exist");
  58.  
  59. }
  60. for(ctr = 0; fp == 0; ctr++)
  61. fread(&(r[ctr]), sizeof(studrec), 1, fp);
  62.  
  63. printf("File Loaded");
  64. getch();
  65. clrscr();
  66. }
Last edited by Whilliam; Jul 2nd, 2009 at 11:44 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: file problem

 
0
  #2
Jul 2nd, 2009
> for(ctr = 0; fp == 0; ctr++)
What does this do?

> flushall();
> gets(filename);
flushall() is non-standard, and gets() is the tool of the devil.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

> #include<conio.h>
Why are you still using this obsolete header?
Is your compiler also obsolete?
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

 
1
  #3
Jul 2nd, 2009
I sense a problem here in your load file function :
  1. for(ctr = 0; fp == 0; ctr++)
  2. fread(&(r[ctr]), sizeof(studrec), 1, fp);

You can correct it as
  1. size_t c;
  2. for(ctr = 0; c != 0; ctr++)
  3. c=fread(&(r[ctr]), sizeof(studrec), 1, fp);
Above code will take one extra loop to find the end so better
  1. size_t c;
  2. for(ctr = 0;; ctr++)
  3. {
  4. c = fread(&(r[ctr]), sizeof(studrec), 1, fp);
  5. if(!c) break;
  6. }
There are far better ways,this is just the least modification of your code.

And sorry salem this is happening second time with me today when I begin writin there would be no post and when I post it there would be a reply already...and we cant delete any post made
Last edited by csurfer; Jul 2nd, 2009 at 12:39 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: file problem

 
0
  #4
Jul 2nd, 2009
most of these trifling problems will go away when you throw away the P.O.S. Borland/Turbo compiler and get a real C/C++ compiler like CodeBlocks or Bloodshed Dev C++.

these GCC-based, standard C compilers wont let you use worthless functions like flushall(), obsolete libraries like conio.h, and will complain heartily if you try and hang yourself by using gets()

.
Last edited by jephthah; Jul 2nd, 2009 at 2:05 pm.
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

 
0
  #5
Jul 2nd, 2009
@Salem:
Im using borland c
fp == 0 in for loop of function load_file is supposed to find the end of file.. I don't know how to do it so I tried that.
Please help me find a better way..

@csurfer
I tried the modified code and it work the first time but when I close the program and run it again and load the file that I saved in the first program, it displays garbage values.
Last edited by Whilliam; Jul 2nd, 2009 at 7:11 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

 
0
  #6
Jul 2nd, 2009
Post in your code or else attach the file.

I told you earlier itself that was just to show you the implementation.Thats it you need to research the things and work out for yourself. We here at Daniweb just show you the way we don't give you every details
Last edited by csurfer; Jul 2nd, 2009 at 7:12 pm.
I Surf in "C"....
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

 
0
  #7
Jul 2nd, 2009
ok here it is..

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4.  
  5. #define max 20
  6.  
  7. typedef struct
  8. {
  9. char LN[16];
  10. char FN[24];
  11. char MI;
  12. }name;
  13.  
  14. typedef struct
  15. {
  16. unsigned long ID;
  17. name n;
  18. char course[8];
  19. int year;
  20. }studrec[max];
  21.  
  22. void input(studrec s, int n);
  23. void view(studrec s, int n);
  24. void save_file(studrec s, int n);
  25. void load_file(studrec r);
  26.  
  27. void input(studrec s, int n)
  28. {
  29. int ctr;
  30.  
  31. for(ctr = 0; ctr < n; ctr++)
  32. {
  33. printf("Input student %d's Last Name [enter] First Name [enter] Middle Initial [enter]: \n", ctr + 1);
  34. flushall();
  35. gets(s[ctr].n.LN);
  36. flushall();
  37. gets(s[ctr].n.FN);
  38. scanf("%c", &s[ctr].n.MI);
  39.  
  40. printf("What is student %d's ID number? ", ctr + 1);
  41. scanf("%d", &s[ctr].ID);
  42.  
  43. printf("What is student %d's course? ", ctr + 1);
  44. flushall();
  45. gets(s[ctr].course);
  46.  
  47. printf("What is student %d's year? ", ctr + 1);
  48. scanf("%d", &s[ctr].year);
  49.  
  50. printf("\n");
  51. }
  52. }
  53.  
  54. void save_file(studrec s, int n)
  55. {
  56. FILE *fp;
  57. char filename[30];
  58. int ctr;
  59.  
  60. printf("What is the name of the file? ");
  61. flushall();
  62. gets(filename);
  63.  
  64. if((fp = fopen(filename, "wb")) == 0)
  65. {
  66. printf("Cannot write file");
  67.  
  68. }
  69. for(ctr = 0; ctr < n; ctr++)
  70. fwrite(&(s[ctr]), sizeof(studrec), 1, fp);
  71.  
  72. printf("File Saved");
  73. fclose(fp);
  74. getch();
  75. clrscr();
  76. }
  77.  
  78. void load_file(studrec r)
  79. {
  80. FILE *fp;
  81. int ctr;
  82. char filename[30];
  83. size_t c;
  84.  
  85. printf("What is the filename? ");
  86. flushall();
  87. gets(filename);
  88. if((fp = fopen(filename, "rb")) == 0)
  89. {
  90. printf("File does not exist");
  91.  
  92. }
  93. for(ctr = 0; c != 0; ctr++)
  94. c=fread(&(r[ctr]), sizeof(studrec), 1, fp);
  95.  
  96. printf("File Loaded");
  97. getch();
  98. clrscr();
  99. }
  100.  
  101. void view(studrec s, int n)
  102. {
  103. int ctr;
  104.  
  105. for(ctr = 0; ctr < n; ctr++)
  106. {
  107. printf("%s, %s %c.\n", s[ctr].n.LN, s[ctr].n.FN, s[ctr].n.MI);
  108. printf("%d\n", s[ctr].ID);
  109. puts(s[ctr].course);
  110. printf("%d\n", s[ctr].year);
  111. printf("\n");
  112. }
  113. }
  114.  
  115. void main(void)
  116. {
  117. studrec s, r;
  118. char ans;
  119. int quit = 0, n;
  120.  
  121. clrscr();
  122.  
  123. do
  124. {
  125. printf("Press\nI for Input\nS to save file\nL to load a file\nV to view\nQ to quit\n");
  126. ans = getch();
  127. clrscr();
  128.  
  129. if(ans == 'I' || ans == 'i')
  130. {
  131. printf("How many records? ");
  132. scanf("%d", &n);
  133. clrscr();
  134.  
  135. input(s, n);
  136. clrscr();
  137. }
  138. else if(ans == 'S' || ans == 's')
  139. save_file(s, n);
  140. else if(ans == 'L' || ans == 'l')
  141. {
  142. load_file(r);
  143. view(r, n);
  144. }
  145. else if(ans == 'V' || ans == 'v')
  146. {
  147. view(s, n);
  148. getch();
  149. clrscr();
  150. }
  151. else if(ans == 'Q' || ans == 'q')
  152. quit = 1;
  153. }while(quit == 0);
  154. }
Last edited by Whilliam; Jul 2nd, 2009 at 7:15 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

 
0
  #8
Jul 2nd, 2009
Some major problems :

1>Get a standard compiler for yourself. They are all free of cost.Boreland C and Tc and all are not going to take you anywhere.

2>The load function works fine better use the second method I had suggested for "for loop" rather than the one implemented.Because the present condition loops one time extra.But still works.Some bug may creep in so the suggestion.

3>Problem is with your view function.You are calling it as view(s,n); .You cannot just pass any value for n you should keep a counter for total number of records in the file and that should be passed as n.Or atleast if you think that is users work to remember the number of records you atleast need to take the input for n before calling view as:
  1. printf("Input the number records you want to view / input the number of records in file that you remember :");
  2. scanf("%d",&n);
  3. view(r,n)
It should be mainly implemented this is the function looping infinitely.With arbitrary value for n it will loop any random number of times.So infinite loop.
Last edited by csurfer; Jul 2nd, 2009 at 7:45 pm.
I Surf in "C"....
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

 
0
  #9
Jul 2nd, 2009
Originally Posted by csurfer View Post
Some major problems :

1>Get a standard compiler for yourself. They are all free of cost.Boreland C and Tc and all are not going to take you anywhere.

2>The load function works fine better use the second method I had suggested for "for loop" rather than the one implemented.Because the present condition loops one time extra.But still works.Some bug may creep in so the suggestion.

3>Problem is with your view function.You are calling it as view(s,n); .You cannot just pass any value for n you should keep a counter for total number of records in the file and that should be passed as n.Or atleast if you think that is users work to remember the number of records you atleast need to take the input for n before calling view as:
  1. printf("Input the number records you want to view / input the number of records in file that you remember :");
  2. scanf("%d",&n);
  3. view(r,n)
It should be mainly implemented this is the function looping infinitely.With arbitrary value for n it will loop any random number of times.So infinite loop.
thanks man.. it worked!
I need to use tc and borland c for now because its what we are using in school.
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

 
0
  #10
Jul 2nd, 2009
Originally Posted by Whilliam View Post
thanks man.. it worked!
I need to use tc and borland c for now because its what we are using in school.
Forget what you are using in school.Those are outdated and useless. Get a good compiler like code blocks or devc++ or VC++ ok.If you work on these standard compilers then you can always work on less standard or totally crap compilers like TC which doesn't give you errors for most of the operations you do.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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