Help about my program. About files

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

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

Help about my program. About files

 
0
  #1
Nov 9th, 2009
Hello, I've been having problems with my program. I really don't get files that much. This is my code:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<conio.h>
  5.  
  6. #define max 3
  7.  
  8. typedef struct
  9. {
  10. char FName[24];
  11. char LName[16];
  12. char MI;
  13. }name;
  14.  
  15. typedef struct
  16. {
  17. unsigned long ID;
  18. name n;
  19. char course[8];
  20. int level;
  21. }profile;
  22.  
  23. typedef struct
  24. {
  25. profile *p;
  26. int last;
  27. }student_record, *srptr;
  28.  
  29. void initialize(srptr *s);
  30. void insert(srptr *s);
  31. void display(srptr s);
  32. void write_file(srptr s);
  33. void read_file(srptr *s);
  34.  
  35. void initialize(srptr *s)
  36. {
  37. *s = (srptr) malloc(sizeof(student_record));
  38. (*s)->p = (profile *) malloc(sizeof(profile) * max);
  39. (*s)->last = -1;
  40. }
  41.  
  42. void insert(srptr *s)
  43. {
  44. strcpy((*s)->p[++(*s)->last].n.FName, "Bradd");
  45. strcpy((*s)->p[(*s)->last].n.LName, "Pitt");
  46. (*s)->p[(*s)->last].n.MI = 'P';
  47. (*s)->p[(*s)->last].ID = 7000000;
  48. strcpy((*s)->p[(*s)->last].course, "BSIT");
  49. (*s)->p[(*s)->last].level = 3;
  50.  
  51. strcpy((*s)->p[++(*s)->last].n.FName, "Angelina");
  52. strcpy((*s)->p[(*s)->last].n.LName, "Jolie");
  53. (*s)->p[(*s)->last].n.MI = 'M';
  54. (*s)->p[(*s)->last].ID = 7111111;
  55. strcpy((*s)->p[(*s)->last].course, "BSICT");
  56. (*s)->p[(*s)->last].level = 1;
  57.  
  58. strcpy((*s)->p[++(*s)->last].n.FName, "Tom");
  59. strcpy((*s)->p[(*s)->last].n.LName, "Cruise");
  60. (*s)->p[(*s)->last].n.MI = 'A';
  61. (*s)->p[(*s)->last].ID = 7222222;
  62. strcpy((*s)->p[(*s)->last].course, "ACTMT");
  63. (*s)->p[(*s)->last].level = 2;
  64. }
  65.  
  66. void display(srptr s)
  67. {
  68. int ctr;
  69.  
  70. for(ctr = 0; ctr < max; ctr++)
  71. {
  72. printf("%d)\n", ctr + 1);
  73. printf("%s %c. %s\n", s->p[ctr].n.FName, s->p[ctr].n.MI, s->p[ctr].n.LName);
  74. printf("%li\n", s->p[ctr].ID);
  75. printf("%s %d\n", s->p[ctr].course, s->p[ctr].level);
  76. printf("\n");
  77. }
  78. }
  79.  
  80. void write_file(srptr s)
  81. {
  82. FILE *fp;
  83. int ctr;
  84.  
  85. if((fp = fopen("test.dat", "wb")) == NULL)
  86. printf("Cannot open file\n");
  87.  
  88. for(ctr = 0; ctr <= s->last; ctr++)
  89. {
  90. fwrite(&s->p[ctr].ID, sizeof(long), 1, fp);
  91. fwrite(&s->p[ctr].n.FName, sizeof(char), strlen(s->p[ctr].n.FName), fp);
  92. fwrite(&s->p[ctr].n.LName, sizeof(char), strlen(s->p[ctr].n.LName), fp);
  93. fwrite(&s->p[ctr].n.MI, sizeof(char), 1, fp);
  94. fwrite(&s->p[ctr].course, sizeof(char), strlen(s->p[ctr].course), fp);
  95. fwrite(&s->p[ctr].level, sizeof(int), 1, fp);
  96. }
  97. printf("File Saved\n");
  98. getch();
  99. clrscr();
  100. fclose(fp);
  101. }
  102.  
  103. void read_file(srptr *s)
  104. {
  105. FILE *fp;
  106. int ctr;
  107.  
  108. if((fp = fopen("test.dat", "rb")) == 0)
  109. printf("File is empty\n");
  110.  
  111. for(ctr = 0; ctr < max; ctr++)
  112. {
  113. fread(&(*s)->p[ctr].ID, sizeof(long), 1, fp);
  114. fread(&(*s)->p[ctr].n.FName, sizeof(char), strlen((*s)->p[ctr].n.FName), fp);
  115. fread(&(*s)->p[ctr].n.LName, sizeof(char), strlen((*s)->p[ctr].n.LName), fp);
  116. fread(&(*s)->p[ctr].n.MI, sizeof(char), 1, fp);
  117. fread(&(*s)->p[ctr].course, sizeof(char), strlen((*s)->p[ctr].course), fp);
  118. fread(&(*s)->p[ctr].level, sizeof(int), 1, fp);
  119. (*s)->last++;
  120. }
  121. printf("File Loaded\n");
  122. getch();
  123. clrscr();
  124. fclose(fp);
  125. }
  126.  
  127. void main()
  128. {
  129. srptr s;
  130. initialize(&s);
  131.  
  132. insert(&s);
  133.  
  134. write_file(s);
  135.  
  136. read_file(&s);
  137.  
  138.  
  139. display(s);
  140. }
I run the program two times. First,
It will write the data that are in function insert to file "test.dat". The read_file function call doesn't exist yet.
The second time I run the program, the function call insert and write_file are deleted and function call read_file is created.

I've been getting garbage value and I can't figure out why. Please help.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 5
Reputation: super-sonic is an unknown quantity at this point 
Solved Threads: 1
super-sonic super-sonic is offline Offline
Newbie Poster
 
0
  #2
Nov 10th, 2009
1. Do not use void main() .
2. Always try to free the memory which is allocated.
3. When you run the program for the second time by deleting insert and write_file , then the strlen function will be garbage. i.e, strlen((*s)->p , [ctr].n.FName)[/icode], , strlen((*s)->p[ctr].n.LName) , and strlen((*s)->p[ctr].course) , in the read_file , function are garbage, hence you get garbage result when you display.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC