reading ints from a binary file

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

Join Date: Jan 2008
Posts: 23
Reputation: hallinan is an unknown quantity at this point 
Solved Threads: 0
hallinan hallinan is offline Offline
Newbie Poster

reading ints from a binary file

 
0
  #1
Feb 11th, 2008
hi I'm doing a banking project using binary files I need to write into a binary file and read and calculate the amounts within it. I open a file by asking the user for their name and searching and a file. I am having trouble reading the integers into a file and calculating.Here is my code so far.I'm also seem to have trouble with a file 'Index'(binary) which stores the users info but when I'm reading the file into a struct only one person is found whilst searching which was the first person entered in the file.Can anyone help me Please?!?!?

  1. <ol style="list-style-type: decimal"><li>#include <stdio.h></li>
  2. <li>#include <string.h></li>
  3. <li>#include <stdlib.h>
  4. </li>
  5. <li>#define MAX 100
  6. </li>
  7. <li>int menu(void);</li>
  8. <li>int display(int i);</li>
  9. <li>void customer_search(void);</li>
  10. <li>void AccNo_search(void);</li>
  11. <li>void createFile(char *);</li>
  12. <li>void openFile(char *);</li>
  13. <li>void enter(void);</li>
  14. <li>void save(void);</li>
  15. <li>void load(void);</li>
  16. <li>struct catalog</li>
  17. <li>{</li>
  18. <li> char name[80];</li>
  19. <li> char AccNo[6];</li>
  20. <li> char address[80];</li>
  21. <li> unsigned date;</li>
  22. <li> unsigned char month;</li>
  23. <li>}*cat[MAX];</li>
  24. <li>struct bank</li>
  25. <li>{</li>
  26. <li> char BankName[20];</li>
  27. <li> unsigned LastAccNum;</li>
  28. <li> unsigned sortCode;</li>
  29. <li>}*bank[MAX];</li>
  30. <li>struct Account</li>
  31. <li>{</li>
  32. <li> int amounts[100];</li>
  33. <li>}*acc[MAX];</li>
  34. <li>int top = 0;
  35. </li>
  36. <li>int main()</li>
  37. <li>{</li>
  38. <li> int choice;</li>
  39. <li> load();</li>
  40. <li> system("CLS");</li>
  41. <li> do</li>
  42. <li> {</li>
  43. <li> choice = menu();</li>
  44. <li> switch(choice)</li>
  45. <li> {</li>
  46. <li> case 1: enter();</li>
  47. <li> break;</li>
  48. <li> case 2: customer_search();</li>
  49. <li> break;</li>
  50. <li> case 3: AccNo_search();</li>
  51. <li> break;</li>
  52. <li> case 4: save();</li>
  53. <li> }</li>
  54. <li> } while(choice !=5);
  55. </li>
  56. <li>system("PAUSE");</li>
  57. <li> return 0;</li>
  58. <li>}</li>
  59. <li> /* Return a menu selection*/</li>
  60. <li>int menu(void)</li>
  61. <li>{</li>
  62. <li> int i;</li>
  63. <li> char str[80];</li>
  64. <li> printf("\t\n\aHallinan Savings Inc.\n\n");</li>
  65. <li> printf("1->Add Customer\n");</li>
  66. <li> printf("2->Search by Name\n");</li>
  67. <li> printf("3->Search by Account Number\n");</li>
  68. <li> printf("4->Save added Customers\n");</li>
  69. <li> printf("5->Quit\n");</li>
  70. <li> do</li>
  71. <li> {</li>
  72. <li> printf("Make your selection: ");</li>
  73. <li> gets(str);</li>
  74. <li> i = atoi(str);</li>
  75. <li> printf("\n");</li>
  76. <li> } while(i < 1 || i > 5);</li>
  77. <li> return i;</li>
  78. <li>}</li>
  79. <li> /*Enter customer into file */</li>
  80. <li>void enter()</li>
  81. <li>{</li>
  82. <li> int i;</li>
  83. <li> char temp[80];</li>
  84. <li> for (i = top; i<MAX ; i++)</li>
  85. <li> {</li>
  86. <li> cat[i] = malloc(sizeof(struct catalog));</li>
  87. <li> if(!cat[i])</li>
  88. <li> {</li>
  89. <li> printf("Out Of Memory\n");</li>
  90. <li> return;</li>
  91. <li> }</li>
  92. <li> printf("Enter customer name (ENTER to Quit):");</li>
  93. <li> gets(cat[i] -> name);</li>
  94. <li> if(!*cat[i] -> name)</li>
  95. <li> break;</li>
  96. <li> createFile(cat[i] -> name);</li>
  97. <li> printf("\nEnter AccNo: ");</li>
  98. <li> gets(cat[i] -> AccNo);</li>
  99. <li> printf("\nEnter address: ");</li>
  100. <li> gets(cat[i] -> address);</li>
  101. <li> printf("\nEnter year: ");</li>
  102. <li> gets(temp);</li>
  103. <li> cat[i] -> date = (unsigned) atoi(temp);</li>
  104. <li> printf("\nEnter month: ");</li>
  105. <li> gets(temp);</li>
  106. <li> cat[i] -> month = (unsigned) atoi(temp);</li>
  107. <li> }</li>
  108. <li> top =i ;</li>
  109. <li>}</li>
  110. <li>/* Search for customer */</li>
  111. <li>void customer_search()</li>
  112. <li>{</li>
  113. <li> char name[80];</li>
  114. <li> char ans;</li>
  115. <li> int i , found;</li>
  116. <li> printf("Name: ");</li>
  117. <li> gets(name);</li>
  118. <li> found = 0;</li>
  119. <li> for (i=0; i<top; i++)</li>
  120. <li> {</li>
  121. <li> if (!strcmp(name,cat[i]->name))</li>
  122. <li> {</li>
  123. <li> display(i);</li>
  124. <li> found = 1;</li>
  125. <li> printf("\n");</li>
  126. <li> printf("\n\nWould you like to Open this customer file?(Y/N)");</li>
  127. <li> scanf("%c",&ans);</li>
  128. <li> if(ans == 'y' || ans =='Y')</li>
  129. <li> {</li>
  130. <li> i-1;</li>
  131. <li> openFile(cat[i]->name);</li>
  132. <li> }</li>
  133. <li> }</li>
  134. <li> }</li>
  135. <li> if(!found)</li>
  136. <li> printf("Not Found\n");</li>
  137. <li>}
  138.  
  139. </li>
  140. <li>void AccNo_search()</li>
  141. <li>{</li>
  142. <li> char AccNo[80],ans;</li>
  143. <li> int i , found;</li>
  144. <li> printf("AccNo: ");</li>
  145. <li> gets(AccNo);</li>
  146. <li> found = 0;</li>
  147. <li> for (i=0; i<top; i++)</li>
  148. <li> {</li>
  149. <li> if (!strcmp(AccNo,cat[i] -> AccNo))</li>
  150. <li> {</li>
  151. <li> display(i);</li>
  152. <li> found = 1;</li>
  153. <li> printf("\n\nWould you like to Open this customer file?(Y/N)");</li>
  154. <li> scanf("%c",&ans);</li>
  155. <li> if(ans == 'y' || ans =='Y')</li>
  156. <li> {</li>
  157. <li> i-1;</li>
  158. <li> openFile(cat[i]->name);</li>
  159. <li> }</li>
  160. <li> printf("\n");</li>
  161. <li> }</li>
  162. <li> }</li>
  163. <li> if(!found)</li>
  164. <li> printf("Not Found\n");</li>
  165. <li>}
  166. </li>
  167. <li> /*Display entries*/</li>
  168. <li>int display(int i)</li>
  169. <li>{</li>
  170. <li> system("CLS");</li>
  171. <li> printf("%s\n",cat[i]->AccNo);</li>
  172. <li> printf("Account Holder Name: %s\n",cat[i]->name);</li>
  173. <li> printf("address: %s\n",cat[i]->address);</li>
  174. <li> printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);</li>
  175. <li>}</li>
  176. <li> /*load file*/</li>
  177. <li>void load(void)</li>
  178. <li>{</li>
  179. <li> FILE *fp;</li>
  180. <li> FILE *fp2;</li>
  181. <li> int i;</li>
  182. <li> if((fp =fopen("index","rb"))==NULL)</li>
  183. <li> {</li>
  184. <li> printf("File does not exist on disk");</li>
  185. <li> return;</li>
  186. <li> }</li>
  187. <li> if(fread(&top,sizeof top,1,fp) !=1)</li>
  188. <li> {</li>
  189. <li> printf("Error Reading count");</li>
  190. <li> }</li>
  191. <li> for (i=0 ; i < top ; i++)</li>
  192. <li> {</li>
  193. <li> cat[i] = malloc(sizeof(struct catalog));</li>
  194. <li> if (!cat[i])</li>
  195. <li> {</li>
  196. <li> printf("Out of Memory->\n");</li>
  197. <li> top = i-1;</li>
  198. <li> break;</li>
  199. <li> }</li>
  200. <li> if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)</li>
  201. <li> {</li>
  202. <li> printf("Error reading customer data->\n");</li>
  203. <li> }</li>
  204. <li> }</li>
  205. <li> if((fp2 =fopen("bank","rb"))== NULL )</li>
  206. <li> {</li>
  207. <li> printf("File does not exist on disk");</li>
  208. <li> return;</li>
  209. <li> }</li>
  210. <li> if(fread(&top,sizeof top,1,fp2) !=1)</li>
  211. <li> {</li>
  212. <li> printf("Error Reading count");</li>
  213. <li> // exit(1);</li>
  214. <li> }</li>
  215. <li> for (i=0 ; i < top ; i++)</li>
  216. <li> {</li>
  217. <li> bank[i] = malloc(sizeof(struct bank));</li>
  218. <li> if (!bank[i])</li>
  219. <li> {</li>
  220. <li> printf("Out of Memory->\n");</li>
  221. <li> top = i-1;</li>
  222. <li> break;</li>
  223. <li> }</li>
  224. <li> if(fread(bank[i], sizeof(struct bank),1,fp2) !=1)</li>
  225. <li> {</li>
  226. <li> printf("Error reading catalog data->\n");</li>
  227. <li> // exit(1);</li>
  228. <li> }</li>
  229. <li> }</li>
  230. <li> fclose(fp2);</li>
  231. <li> fclose(fp);</li>
  232. <li>}
  233. </li>
  234. <li>/* Save the catalog file */</li>
  235. <li>void save()</li>
  236. <li>{</li>
  237. <li> FILE *fp;</li>
  238. <li> int i;</li>
  239. <li> if((fp =fopen("index","w+"))==NULL)</li>
  240. <li> {</li>
  241. <li> printf("Cant open file->\n");</li>
  242. <li> exit(1);</li>
  243. <li> }</li>
  244. <li> if(fwrite(&top,sizeof top,1,fp) !=1)</li>
  245. <li> {</li>
  246. <li> printf("Error Writting count.\n");</li>
  247. <li> exit(1);</li>
  248. <li> }</li>
  249. <li> for(i=0 ; i < top ; i++)</li>
  250. <li> {</li>
  251. <li> if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)</li>
  252. <li> printf("Error writing count.\n");</li>
  253. <li> }</li>
  254. <li> fclose(fp);</li>
  255. <li>}</li>
  256. <li>void createFile(char *filename)</li>
  257. <li>{</li>
  258. <li> FILE *fp;</li>
  259. <li> int i;</li>
  260. <li> if((fp =fopen(filename,"wb+"))==NULL)</li>
  261. <li> {</li>
  262. <li> printf("\nCant open file->\n");</li>
  263. <li> exit(1);</li>
  264. <li> }</li>
  265. <li> if(fwrite(&top,sizeof top,1,fp) !=1)</li>
  266. <li> {</li>
  267. <li> printf("\nError Writting count.\n");</li>
  268. <li> }</li>
  269. <li> for(i=0 ; i < top ; i++)</li>
  270. <li> {</li>
  271. <li> if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)</li>
  272. <li> printf("\nError writing count.\n");</li>
  273. <li> }</li>
  274. <li> fclose(fp);</li>
  275. <li>}
  276. </li>
  277. <li>void openFile(char *file)</li>
  278. <li>{</li>
  279. <li> FILE *fp;</li>
  280. <li> int i;</li>
  281. <li> printf("%s OPENED!!",file);</li>
  282. <li> if((fp =fopen(file,"rb"))==NULL)</li>
  283. <li> {</li>
  284. <li> printf("\nFile does not exist on disk");</li>
  285. <li> return;</li>
  286. <li> }</li>
  287. <li> if(fread(&top,sizeof top,1,fp) !=1)</li>
  288. <li> {</li>
  289. <li> printf("\nError Reading count");</li>
  290. <li> }</li>
  291. <li> for (i=0 ; i < top ; i++)</li>
  292. <li> {</li>
  293. <li> acc[i] = malloc(sizeof(struct Account));</li>
  294. <li> if (!acc[i])</li>
  295. <li> {</li>
  296. <li> printf("\nOut of Memory->\n");</li>
  297. <li> top = i-1;</li>
  298. <li> break;</li>
  299. <li> }</li>
  300. <li> if(fread(acc[i], sizeof(struct Account),1,fp) !=1)</li>
  301. <li> {</li>
  302. <li> printf("\nError reading customer data->\n");</li>
  303. <li> }</li>
  304. <li> }</li>
  305. <li>}</li>
  306. </ol>
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: reading ints from a binary file

 
0
  #2
Feb 11th, 2008
Unfortunately, your code is very hard to follow. Indentation is necessary for readability, but you went to the extreme and lost readability. See this for formatting help.

Also, you are using a very dangerous command -- gets() -- click to see why.
And a few questionable commands:
system("pause");
scanf("%c", ...);

It also help to give details, as specified in the post Read Me: Read This Before Posting
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
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: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: reading ints from a binary file

 
0
  #3
Feb 12th, 2008
yeah, you cant post 300 lines of code and expect people to sift through it. even if it didnt wrap across line breaks.

try parse out your specific questions, and make a simple example of what your main (or first) problem is. then work from there.
Last edited by jephthah; Feb 12th, 2008 at 2:26 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: reading ints from a binary file

 
0
  #4
Feb 12th, 2008
There are a lot of pointers in your code. Try to debug them.
An Apple a Day keeps a Doctor away!
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



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

©2003 - 2009 DaniWeb® LLC