Code:
#include <stdio.h>
#include <stdlib.h>
#define BINFILE "finance.bin"
typedef struct RECORDS_OF_INTREST
{
int id;
char name[200];
char address [300];
double acc_bal;
}rec_of_intrest;
rec_of_intrest set_of_rec[400];
int main(int argc, const char **argv)
{
int cnt = 0;
// read flat file for data input
FILE *fp;
//for binary files
FILE *bfp;
if ( ( fp = fopen(argv[1], "r") ) == NULL )
{
printf("No File present\n"
"Contact head of state\n"
"to see what happened to it.\n"
"Phone Number should be in the\n"
"yellow pages\n");
exit(1);
}
if (( bfp = fopen(BINFILE, "ab+") ) == NULL)
{
printf("No File present\n"
"Contact head of state\n"
"to see what happened to it.\n"
"Phone Number should be in the\n"
"yellow pages\n");
exit(1);
} // string with spaces unknown amount %*s %[^\n] end line
// adding that it no longer gets first line
char buff[1024]; // 4 fgets to null of file
while ( fscanf(fp, "%d%s%*s%299[^\n]%lf",&set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,&set_of_rec[cnt].acc_bal)
&& fgets(buff, sizeof buff, fp) != NULL)
{
fwrite(&set_of_rec[cnt].id, sizeof(int), 1,bfp);
fwrite(&set_of_rec[cnt].name, sizeof(char),1,bfp);
fwrite(&set_of_rec[cnt].address, sizeof(char),1,bfp);
fwrite(&set_of_rec[cnt].acc_bal, sizeof(double),1,bfp);
// it's suppose to just shove all of the struct data into the bin file at one time
//fwrite(&set_of_rec,sizeof(set_of_rec), 1, bfp);
printf("cnt %d: %d %s %s %.2lf\n",cnt,set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,set_of_rec[cnt].acc_bal);
cnt++;
}
fclose(fp);
fclose(bfp);
printf("\nHere comes struct array\n");
for ( int a =0;a<cnt;a++)
printf("cnt %d: %d %s %s %.2lf\n",cnt,set_of_rec[a].id,set_of_rec[a].name,set_of_rec[a].address,set_of_rec[a].acc_bal);
printf("\nDone from struct array\n");
if ( (bfp = fopen(BINFILE, "wb") ) == NULL)
{
printf("Someone took it, or I just cannot\n"
"access the file. Notify Head of State\n");
exit(1);
}
printf("Id, Name Address, amount in account\n");
cnt = 0;
while (fread(&set_of_rec, sizeof(set_of_rec), 1, fp) > 0) {
printf("\n%d %s %s %.2lf", set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,set_of_rec[cnt].acc_bal);
cnt++;
}
fclose(bfp);
return 0;
}
some of the output off loop and in the struct array itself