| | |
reading ints from a binary file
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 23
Reputation:
Solved Threads: 0
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?!?!?
C Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>#include <stdio.h></li> <li>#include <string.h></li> <li>#include <stdlib.h> </li> <li>#define MAX 100 </li> <li>int menu(void);</li> <li>int display(int i);</li> <li>void customer_search(void);</li> <li>void AccNo_search(void);</li> <li>void createFile(char *);</li> <li>void openFile(char *);</li> <li>void enter(void);</li> <li>void save(void);</li> <li>void load(void);</li> <li>struct catalog</li> <li>{</li> <li> char name[80];</li> <li> char AccNo[6];</li> <li> char address[80];</li> <li> unsigned date;</li> <li> unsigned char month;</li> <li>}*cat[MAX];</li> <li>struct bank</li> <li>{</li> <li> char BankName[20];</li> <li> unsigned LastAccNum;</li> <li> unsigned sortCode;</li> <li>}*bank[MAX];</li> <li>struct Account</li> <li>{</li> <li> int amounts[100];</li> <li>}*acc[MAX];</li> <li>int top = 0; </li> <li>int main()</li> <li>{</li> <li> int choice;</li> <li> load();</li> <li> system("CLS");</li> <li> do</li> <li> {</li> <li> choice = menu();</li> <li> switch(choice)</li> <li> {</li> <li> case 1: enter();</li> <li> break;</li> <li> case 2: customer_search();</li> <li> break;</li> <li> case 3: AccNo_search();</li> <li> break;</li> <li> case 4: save();</li> <li> }</li> <li> } while(choice !=5); </li> <li>system("PAUSE");</li> <li> return 0;</li> <li>}</li> <li> /* Return a menu selection*/</li> <li>int menu(void)</li> <li>{</li> <li> int i;</li> <li> char str[80];</li> <li> printf("\t\n\aHallinan Savings Inc.\n\n");</li> <li> printf("1->Add Customer\n");</li> <li> printf("2->Search by Name\n");</li> <li> printf("3->Search by Account Number\n");</li> <li> printf("4->Save added Customers\n");</li> <li> printf("5->Quit\n");</li> <li> do</li> <li> {</li> <li> printf("Make your selection: ");</li> <li> gets(str);</li> <li> i = atoi(str);</li> <li> printf("\n");</li> <li> } while(i < 1 || i > 5);</li> <li> return i;</li> <li>}</li> <li> /*Enter customer into file */</li> <li>void enter()</li> <li>{</li> <li> int i;</li> <li> char temp[80];</li> <li> for (i = top; i<MAX ; i++)</li> <li> {</li> <li> cat[i] = malloc(sizeof(struct catalog));</li> <li> if(!cat[i])</li> <li> {</li> <li> printf("Out Of Memory\n");</li> <li> return;</li> <li> }</li> <li> printf("Enter customer name (ENTER to Quit):");</li> <li> gets(cat[i] -> name);</li> <li> if(!*cat[i] -> name)</li> <li> break;</li> <li> createFile(cat[i] -> name);</li> <li> printf("\nEnter AccNo: ");</li> <li> gets(cat[i] -> AccNo);</li> <li> printf("\nEnter address: ");</li> <li> gets(cat[i] -> address);</li> <li> printf("\nEnter year: ");</li> <li> gets(temp);</li> <li> cat[i] -> date = (unsigned) atoi(temp);</li> <li> printf("\nEnter month: ");</li> <li> gets(temp);</li> <li> cat[i] -> month = (unsigned) atoi(temp);</li> <li> }</li> <li> top =i ;</li> <li>}</li> <li>/* Search for customer */</li> <li>void customer_search()</li> <li>{</li> <li> char name[80];</li> <li> char ans;</li> <li> int i , found;</li> <li> printf("Name: ");</li> <li> gets(name);</li> <li> found = 0;</li> <li> for (i=0; i<top; i++)</li> <li> {</li> <li> if (!strcmp(name,cat[i]->name))</li> <li> {</li> <li> display(i);</li> <li> found = 1;</li> <li> printf("\n");</li> <li> printf("\n\nWould you like to Open this customer file?(Y/N)");</li> <li> scanf("%c",&ans);</li> <li> if(ans == 'y' || ans =='Y')</li> <li> {</li> <li> i-1;</li> <li> openFile(cat[i]->name);</li> <li> }</li> <li> }</li> <li> }</li> <li> if(!found)</li> <li> printf("Not Found\n");</li> <li>} </li> <li>void AccNo_search()</li> <li>{</li> <li> char AccNo[80],ans;</li> <li> int i , found;</li> <li> printf("AccNo: ");</li> <li> gets(AccNo);</li> <li> found = 0;</li> <li> for (i=0; i<top; i++)</li> <li> {</li> <li> if (!strcmp(AccNo,cat[i] -> AccNo))</li> <li> {</li> <li> display(i);</li> <li> found = 1;</li> <li> printf("\n\nWould you like to Open this customer file?(Y/N)");</li> <li> scanf("%c",&ans);</li> <li> if(ans == 'y' || ans =='Y')</li> <li> {</li> <li> i-1;</li> <li> openFile(cat[i]->name);</li> <li> }</li> <li> printf("\n");</li> <li> }</li> <li> }</li> <li> if(!found)</li> <li> printf("Not Found\n");</li> <li>} </li> <li> /*Display entries*/</li> <li>int display(int i)</li> <li>{</li> <li> system("CLS");</li> <li> printf("%s\n",cat[i]->AccNo);</li> <li> printf("Account Holder Name: %s\n",cat[i]->name);</li> <li> printf("address: %s\n",cat[i]->address);</li> <li> printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);</li> <li>}</li> <li> /*load file*/</li> <li>void load(void)</li> <li>{</li> <li> FILE *fp;</li> <li> FILE *fp2;</li> <li> int i;</li> <li> if((fp =fopen("index","rb"))==NULL)</li> <li> {</li> <li> printf("File does not exist on disk");</li> <li> return;</li> <li> }</li> <li> if(fread(&top,sizeof top,1,fp) !=1)</li> <li> {</li> <li> printf("Error Reading count");</li> <li> }</li> <li> for (i=0 ; i < top ; i++)</li> <li> {</li> <li> cat[i] = malloc(sizeof(struct catalog));</li> <li> if (!cat[i])</li> <li> {</li> <li> printf("Out of Memory->\n");</li> <li> top = i-1;</li> <li> break;</li> <li> }</li> <li> if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)</li> <li> {</li> <li> printf("Error reading customer data->\n");</li> <li> }</li> <li> }</li> <li> if((fp2 =fopen("bank","rb"))== NULL )</li> <li> {</li> <li> printf("File does not exist on disk");</li> <li> return;</li> <li> }</li> <li> if(fread(&top,sizeof top,1,fp2) !=1)</li> <li> {</li> <li> printf("Error Reading count");</li> <li> // exit(1);</li> <li> }</li> <li> for (i=0 ; i < top ; i++)</li> <li> {</li> <li> bank[i] = malloc(sizeof(struct bank));</li> <li> if (!bank[i])</li> <li> {</li> <li> printf("Out of Memory->\n");</li> <li> top = i-1;</li> <li> break;</li> <li> }</li> <li> if(fread(bank[i], sizeof(struct bank),1,fp2) !=1)</li> <li> {</li> <li> printf("Error reading catalog data->\n");</li> <li> // exit(1);</li> <li> }</li> <li> }</li> <li> fclose(fp2);</li> <li> fclose(fp);</li> <li>} </li> <li>/* Save the catalog file */</li> <li>void save()</li> <li>{</li> <li> FILE *fp;</li> <li> int i;</li> <li> if((fp =fopen("index","w+"))==NULL)</li> <li> {</li> <li> printf("Cant open file->\n");</li> <li> exit(1);</li> <li> }</li> <li> if(fwrite(&top,sizeof top,1,fp) !=1)</li> <li> {</li> <li> printf("Error Writting count.\n");</li> <li> exit(1);</li> <li> }</li> <li> for(i=0 ; i < top ; i++)</li> <li> {</li> <li> if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)</li> <li> printf("Error writing count.\n");</li> <li> }</li> <li> fclose(fp);</li> <li>}</li> <li>void createFile(char *filename)</li> <li>{</li> <li> FILE *fp;</li> <li> int i;</li> <li> if((fp =fopen(filename,"wb+"))==NULL)</li> <li> {</li> <li> printf("\nCant open file->\n");</li> <li> exit(1);</li> <li> }</li> <li> if(fwrite(&top,sizeof top,1,fp) !=1)</li> <li> {</li> <li> printf("\nError Writting count.\n");</li> <li> }</li> <li> for(i=0 ; i < top ; i++)</li> <li> {</li> <li> if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)</li> <li> printf("\nError writing count.\n");</li> <li> }</li> <li> fclose(fp);</li> <li>} </li> <li>void openFile(char *file)</li> <li>{</li> <li> FILE *fp;</li> <li> int i;</li> <li> printf("%s OPENED!!",file);</li> <li> if((fp =fopen(file,"rb"))==NULL)</li> <li> {</li> <li> printf("\nFile does not exist on disk");</li> <li> return;</li> <li> }</li> <li> if(fread(&top,sizeof top,1,fp) !=1)</li> <li> {</li> <li> printf("\nError Reading count");</li> <li> }</li> <li> for (i=0 ; i < top ; i++)</li> <li> {</li> <li> acc[i] = malloc(sizeof(struct Account));</li> <li> if (!acc[i])</li> <li> {</li> <li> printf("\nOut of Memory->\n");</li> <li> top = i-1;</li> <li> break;</li> <li> }</li> <li> if(fread(acc[i], sizeof(struct Account),1,fp) !=1)</li> <li> {</li> <li> printf("\nError reading customer data->\n");</li> <li> }</li> <li> }</li> <li>}</li> </ol>
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- fstream Tutorial (C++)
- What errors can occur while reading a file? (C)
- Signal Processing: Hadamard Matrices (C)
- file i/o irregularity (C)
Other Threads in the C Forum
- Previous Thread: C Assignment
- Next Thread: about accessing
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h






