User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,785 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,516 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 398 | Replies: 7
Reply
Join Date: May 2008
Posts: 16
Reputation: GigaCorp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
GigaCorp GigaCorp is offline Offline
Newbie Poster

fread/fwrite

  #1  
May 10th, 2008
ok i got past that part i posted about before, now im having some more trouble. i can add 2 patients easily to the file. then i try to display them. it displays the first one just fine, but the second it tries to display the first value in the [1]th element of the array, i get an error message. that part of my code is marked with a /*error*/ just before it. easy to find plz help this is due monday.

heres my code
  1. #include "iostream"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include "process.h"
  6. #define maxpatients 8
  7. using namespace std;
  8.  
  9. void main();
  10.  
  11.  
  12.  
  13. struct namestruct
  14. {
  15. char firstName[32];
  16. char middleI;
  17. char lastName[32];
  18. };
  19.  
  20. struct addressstruct
  21. {
  22. char streetAddress[32];
  23. char city[32];
  24. char state[3];
  25. int zipcode;
  26. };
  27.  
  28. struct locationstruct
  29. {
  30. int numFloor;
  31. int numRoom;
  32. unsigned char subLocation;
  33. };
  34.  
  35. struct geninfostruct
  36. {
  37. addressstruct address;
  38. char phoneNumber[12];
  39. };
  40.  
  41. struct insuranceinfostruct
  42. {
  43. char nameCarrier[32];
  44. geninfostruct insurancegeninfo;
  45. float deductable;
  46. };
  47.  
  48. struct datestruct
  49. {
  50. int day;
  51. int month;
  52. int year;
  53. };
  54.  
  55. struct patientstruct
  56. {
  57. namestruct namepatient;
  58. int age;
  59. geninfostruct geninfopatient;
  60. insuranceinfostruct insurence;
  61. datestruct dischargedate;
  62. locationstruct locationpatient;
  63. char information[1024];
  64. };
  65.  
  66.  
  67.  
  68. char menu()
  69. {
  70. char option1;
  71. do
  72. {
  73. system("cls");
  74. printf("press a coresponding key and then press enter to choose an option:\n1. open a file\n2. write database to file\n3. display patients\n4. find a patient\n5. add a patient\n6. delete a patient\n7. move a patient\n8.print out patients by discharge date\n");
  75. option1 = getchar();
  76. }
  77. while (option1<'1' || option1 > '7');
  78.  
  79. fflush(stdin);
  80.  
  81. return option1;
  82. }
  83.  
  84. void addpatient(patientstruct (*patientstructpointer)[maxpatients], int (*count)=0)
  85. {
  86. patientstruct patientstruct1[maxpatients];
  87. int numread;
  88. char yesno;
  89. FILE *fp;
  90. fp=fopen("c:\\patientdirectory.txt","r");
  91. if(fp==NULL)
  92. {
  93. printf("file not found. create one?y/n");
  94. fflush(stdin);
  95. do
  96. {
  97. // scanf_s("%c",&yesno);
  98. cin>>yesno;
  99. switch(yesno)
  100. {
  101. case 'y':
  102. break;
  103. case 'n':
  104. main();
  105. break;
  106. default:
  107. printf("please enter y for yes or n for no and press enter");
  108. break;
  109. }
  110. }while(yesno!='y');
  111. }
  112. else
  113. {
  114. while (!feof(fp))
  115. {
  116. numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
  117. (*count)++;
  118. }
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. system("cls");
  129.  
  130. fflush(stdin);
  131. printf("\nEnter the patient's first name\n");
  132. gets_s((*patientstructpointer)[*count].namepatient.firstName);
  133. fflush(stdin);
  134. printf("\nEnter the patient's middle initial\n");
  135. (*patientstructpointer)[*count].namepatient.middleI=getchar();
  136. fflush(stdin);
  137. printf("\nEnter the patient's last name\n");
  138. gets_s((*patientstructpointer)[*count].namepatient.firstName);
  139. fflush(stdin);
  140. printf("\nEnter the patient's age\n");
  141. scanf_s("%d",&(*patientstructpointer)[*count].age);
  142. fflush(stdin);
  143. printf("\nEnter the patient's state of residence\n");
  144. gets_s((*patientstructpointer)[*count].geninfopatient.address.state);
  145. printf("\nEnter the patient's city of residence\n");
  146. gets_s((*patientstructpointer)[*count].geninfopatient.address.city);
  147. fflush(stdin);
  148. printf("\nEnter the patient's zipcode of residence\n");
  149. scanf_s("%d",&(*patientstructpointer)[*count].geninfopatient.address.zipcode);
  150. fflush(stdin);
  151. printf("\nEnter the patient's street address of residence\n");
  152. gets_s((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
  153. fflush(stdin);
  154. printf("\nEnter the patient's phone number\n");
  155. gets_s((*patientstructpointer)[*count].geninfopatient.phoneNumber);
  156. fflush(stdin);
  157. printf("\nEnter the patient's insurance carrier's name\n");
  158. gets_s((*patientstructpointer)[*count].insurence.nameCarrier);
  159. fflush(stdin);
  160. printf("\nEnter the patient's insurance carrier's state\n");
  161. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
  162. fflush(stdin);
  163. printf("\nEnter the patient's insurance carrier's city\n");
  164. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
  165. fflush(stdin);
  166. printf("\nEnter the patient's insurance carrier's zipcode\n");
  167. scanf_s("%d",&(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
  168. fflush(stdin);
  169. printf("\nEnter the patient's insurance carrier's street address\n");
  170. gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
  171. fflush(stdin);
  172. printf("\nEnter the patient's insurance carrier's deductable\n");
  173. scanf_s("%f",&(*patientstructpointer)[*count].insurence.deductable);
  174. fflush(stdin);
  175. printf("\nEnter the patient's year of discharge\n");
  176. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.year);
  177. fflush(stdin);
  178. printf("\nEnter the patient's month of discharge\n");
  179. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.month);
  180. fflush(stdin);
  181. printf("\nEnter the patient's day of discharge\n");
  182. scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.day);
  183. fflush(stdin);
  184. printf("\nEnter the patient's floor number\n");
  185. scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numFloor);
  186. fflush(stdin);
  187. printf("\nEnter the patient's room number\n");
  188. scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numRoom);
  189. fflush(stdin);
  190. printf("\nEnter the patient's location number\n");
  191. //location
  192. ((*patientstructpointer)[*count].locationpatient.subLocation)=1;
  193. int locationnumber;
  194. scanf_s("%d",&locationnumber);
  195. switch(locationnumber)
  196. {
  197. case 1:
  198. break;
  199. case '2':
  200. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<1;
  201. break;
  202. case '3':
  203. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<2;
  204. break;
  205. case '4':
  206. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<3;
  207. break;
  208. case '5':
  209. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<4;
  210. break;
  211. case '6':
  212. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<5;
  213. break;
  214. case '7':
  215. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<6;
  216. break;
  217. case '8':
  218. ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<7;
  219. break;
  220. default:
  221. printf("you're fired");
  222. }
  223. fflush(stdin);
  224. printf("\nEnter the patient's description (less than 1023 characters please)\n");
  225. gets_s((*patientstructpointer)[*count].information);
  226. fp=fopen("c:\\patientdirectory.txt","a");
  227. /**/fwrite ((patientstructpointer)[*count],sizeof((*patientstructpointer)),1,fp);
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234. void displaypatients()
  235. {
  236. FILE *fp;
  237. fp=fopen("c:\\patientdirectory.txt","r");
  238. patientstruct (*patientstructpointer)[maxpatients];
  239. patientstruct (patientstruct1)[maxpatients];
  240. (patientstructpointer)=&(patientstruct1);
  241. int numread;
  242. int *count;
  243. int asa;
  244. count=&asa;
  245. asa=0;
  246. while (!feof(fp))
  247. {
  248. numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
  249. fflush(stdin);
  250. printf("\npatient's first name\n");
  251. /*ERROR*/ puts((*patientstructpointer)[*count].namepatient.firstName);
  252. fflush(stdin);
  253. printf("\npatient's middle initial\n");
  254. printf("%c",(*patientstructpointer)[*count].namepatient.middleI);
  255. fflush(stdin);
  256. printf("\npatient's last name\n");
  257. puts((*patientstructpointer)[*count].namepatient.firstName);
  258. fflush(stdin);
  259. printf("\npatient's age\n");
  260. printf("%d",(*patientstructpointer)[*count].age);
  261. fflush(stdin);
  262. printf("\npatient's state of residence\n");
  263. puts((*patientstructpointer)[*count].geninfopatient.address.state);
  264. printf("\npatient's city of residence\n");
  265. puts((*patientstructpointer)[*count].geninfopatient.address.city);
  266. fflush(stdin);
  267. printf("\npatient's zipcode of residence\n");
  268. printf("%d",(*patientstructpointer)[*count].geninfopatient.address.zipcode);
  269. fflush(stdin);
  270. printf("\npatient's street address of residence\n");
  271. puts((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
  272. fflush(stdin);
  273. printf("\npatient's phone number\n");
  274. puts((*patientstructpointer)[*count].geninfopatient.phoneNumber);
  275. fflush(stdin);
  276. printf("\npatient's insurance carrier's name\n");
  277. puts((*patientstructpointer)[*count].insurence.nameCarrier);
  278. fflush(stdin);
  279. printf("\npatient's insurance carrier's state\n");
  280. puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
  281. fflush(stdin);
  282. printf("\npatient's insurance carrier's city\n");
  283. puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
  284. fflush(stdin);
  285. printf("\npatient's insurance carrier's zipcode\n");
  286. printf("%d",(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
  287. fflush(stdin);
  288. printf("\npatient's insurance carrier's street address\n");
  289. puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
  290. fflush(stdin);
  291. printf("\npatient's insurance carrier's deductable\n");
  292. printf("%f",(*patientstructpointer)[*count].insurence.deductable);
  293. fflush(stdin);
  294. printf("\npatient's year of discharge\n");
  295. printf("%d",(*patientstructpointer)[*count].dischargedate.year);
  296. fflush(stdin);
  297. printf("\npatient's month of discharge\n");
  298. printf("%d",(*patientstructpointer)[*count].dischargedate.month);
  299. fflush(stdin);
  300. printf("\npatient's day of discharge\n");
  301. printf("%d",(*patientstructpointer)[*count].dischargedate.day);
  302. fflush(stdin);
  303. printf("\npatient's floor number\n");
  304. printf("%d",(*patientstructpointer)[*count].locationpatient.numFloor);
  305. fflush(stdin);
  306. printf("\npatient's room number\n");
  307. printf("%d",(*patientstructpointer)[*count].locationpatient.numRoom);
  308. fflush(stdin);
  309. printf("\npatient's location number\n");
  310. //location
  311. int locationnumber;
  312. switch((*patientstructpointer)[*count].locationpatient.subLocation)
  313. {
  314. case 1:
  315. locationnumber=1;
  316. break;
  317. case (1<<1):
  318. locationnumber=2;
  319. break;
  320. case (1<<2):
  321. locationnumber=3;
  322. break;
  323. case (1<<3):
  324. locationnumber=4;
  325. break;
  326. case (1<<4):
  327. locationnumber=5;
  328. break;
  329. case (1<<5):
  330. locationnumber=6;
  331. break;
  332. case (1<<6):
  333. locationnumber=7;
  334. break;
  335. case (1<<7):
  336. locationnumber=8;
  337. break;
  338. default:
  339. locationnumber=0;
  340. printf("you're fired");
  341. }
  342. printf("%d",locationnumber);
  343. fflush(stdin);
  344. printf("\npatient's description (less than 1023 characters please)\n");
  345. puts((*patientstructpointer)[*count].information);
  346. (*count)++;
  347. }
  348.  
  349. printf("\npress any key to continue\n");
  350. getchar();
  351. }
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359. void main()
  360. {
  361. patientstruct patientstruct1[maxpatients];
  362. char entry;
  363. int count = 0;
  364. do
  365. {
  366. entry = menu();
  367. switch(entry)
  368. {
  369. case '1':
  370.  
  371. break;
  372. case '2':
  373.  
  374. break;
  375. case '3':
  376. displaypatients();
  377. break;
  378. case '4':
  379.  
  380. break;
  381. case '5':
  382. addpatient(&patientstruct1,&count);
  383. break;
  384. case '6':
  385.  
  386. break;
  387. case '7':
  388.  
  389. break;
  390. case '8':
  391.  
  392. break;
  393. default:
  394. printf("you're fired");
  395. }
  396. }
  397. while (entry != '7');
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404. for(;;);
  405. }
  406.  
Last edited by Ancient Dragon : May 10th, 2008 at 8:01 pm. Reason: add line numbers for convenience
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: fread/fwrite

  #2  
May 10th, 2008
>>fflush(stdin);
fflush is not defined for input streams, only output, so the above may or may not work.

Is this supposed to be a C or a C++ program. Looks like C, but you have tossed in iostream for some unknown reason.

Whay did you write all that unnecessary pointer stuff ?? All it does is obfuscate your program.

Your use of gets_s() is incorrect. It's supposed to have two parameters, not one. The second parameter is the size of the input buffer.

The while loop starting on line 114 is incorrect. Here's how it should be coded: Also check out how the sizeof operator is coded in the line below.
		while (fread(&patientstructpointer[*count],sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}
Last edited by Ancient Dragon : May 10th, 2008 at 8:17 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,548
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: fread/fwrite

  #3  
May 10th, 2008
Here is how that whole function should have been coded to simplify all those pointers. I left in all those fflush(stdin) lines because I was too lazy to remove them. I also didn't change all those gets_s() function calls for the same reason.

  1. void addpatient(patientstruct patientstructpointer[maxpatients], int (*count)=0)
  2. {
  3. // patientstruct patientstruct1[maxpatients];
  4. int numread;
  5. char yesno;
  6. FILE *fp;
  7. fp=fopen("c:\\patientdirectory.txt","r");
  8. if(fp==NULL)
  9. {
  10. printf("file not found. create one?y/n");
  11. fflush(stdin);
  12. do
  13. {
  14. // scanf_s("%c",&yesno);
  15. cin>>yesno;
  16. switch(yesno)
  17. {
  18. case 'y':
  19. break;
  20. case 'n':
  21. main();
  22. break;
  23. default:
  24. printf("please enter y for yes or n for no and press enter");
  25. break;
  26. }
  27. }while(yesno!='y');
  28. }
  29. else
  30. {
  31. while (fread(&patientstructpointer[*count],
  32. sizeof(patientstructpointer[0]),1,fp) > 0)
  33. {
  34. (*count)++;
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. system("cls");
  46.  
  47. printf("\nEnter the patient's first name\n");
  48. gets_s(patientstructpointer[*count].namepatient.firstName);
  49. printf("\nEnter the patient's middle initial\n");
  50. patientstructpointer[*count].namepatient.middleI = getchar();
  51. printf("\nEnter the patient's last name\n");
  52. gets_s(patientstructpointer[*count].namepatient.firstName);
  53. printf("\nEnter the patient's age\n");
  54. scanf_s("%d",&patientstructpointer[*count].age);
  55. fflush(stdin);
  56. printf("\nEnter the patient's state of residence\n");
  57. gets_s(patientstructpointer[*count].geninfopatient.address.state);
  58. printf("\nEnter the patient's city of residence\n");
  59. gets_s(patientstructpointer[*count].geninfopatient.address.city);
  60. fflush(stdin);
  61. printf("\nEnter the patient's zipcode of residence\n");
  62. scanf_s("%d",&patientstructpointer[*count].geninfopatient.address.zipcode);
  63. fflush(stdin);
  64. printf("\nEnter the patient's street address of residence\n");
  65. gets_s(patientstructpointer[*count].geninfopatient.address.streetAddress);
  66. fflush(stdin);
  67. printf("\nEnter the patient's phone number\n");
  68. gets_s(patientstructpointer[*count].geninfopatient.phoneNumber);
  69. fflush(stdin);
  70. printf("\nEnter the patient's insurance carrier's name\n");
  71. gets_s(patientstructpointer[*count].insurence.nameCarrier);
  72. fflush(stdin);
  73. printf("\nEnter the patient's insurance carrier's state\n");
  74. gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.state);
  75. fflush(stdin);
  76. printf("\nEnter the patient's insurance carrier's city\n");
  77. gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.city);
  78. fflush(stdin);
  79. printf("\nEnter the patient's insurance carrier's zipcode\n");
  80. scanf_s("%d",&patientstructpointer[*count].insurence.insurancegeninfo.address.zipcode);
  81. fflush(stdin);
  82. printf("\nEnter the patient's insurance carrier's street address\n");
  83. gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.streetAddress);
  84. fflush(stdin);
  85. printf("\nEnter the patient's insurance carrier's deductable\n");
  86. scanf_s("%f",&patientstructpointer[*count].insurence.deductable);
  87. fflush(stdin);
  88. printf("\nEnter the patient's year of discharge\n");
  89. scanf_s("%d",&patientstructpointer[*count].dischargedate.year);
  90. fflush(stdin);
  91. printf("\nEnter the patient's month of discharge\n");
  92. scanf_s("%d",&patientstructpointer[*count].dischargedate.month);
  93. fflush(stdin);
  94. printf("\nEnter the patient's day of discharge\n");
  95. scanf_s("%d",&patientstructpointer[*count].dischargedate.day);
  96. fflush(stdin);
  97. printf("\nEnter the patient's floor number\n");
  98. scanf_s("%d",&patientstructpointer[*count].locationpatient.numFloor);
  99. fflush(stdin);
  100. printf("\nEnter the patient's room number\n");
  101. scanf_s("%d",&patientstructpointer[*count].locationpatient.numRoom);
  102. fflush(stdin);
  103. printf("\nEnter the patient's location number\n");
  104. //location
  105. (patientstructpointer[*count].locationpatient.subLocation)=1;
  106. int locationnumber;
  107. scanf_s("%d",&locationnumber);
  108. switch(locationnumber)
  109. {
  110. case 1:
  111. break;
  112. case '2':
  113. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<1;
  114. break;
  115. case '3':
  116. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<2;
  117. break;
  118. case '4':
  119. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<3;
  120. break;
  121. case '5':
  122. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<4;
  123. break;
  124. case '6':
  125. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<5;
  126. break;
  127. case '7':
  128. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<6;
  129. break;
  130. case '8':
  131. (patientstructpointer[*count].locationpatient.subLocation)=(patientstructpointer[*count].locationpatient.subLocation)<<7;
  132. break