Code:
#include <stdio.h>
#include <stdlib.h>
#define BINFILE "black_ops.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)
{
// if use does not add anything other than program name.
if ( argc < 2)
{
printf("Usage:\n"
"%s [data file]\n"
"your output file is \n"
"named %s\n", argv[0], BINFILE);
return -1;
}
int cnt = 0 , amount=0;
//int len = 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, "wb+") ) == 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)
{
// to just shove all of the struct data into the bin file all 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);
if ( (bfp = fopen(BINFILE, "rb+") ) == NULL)
{
printf("Someone took it, or I just cannot\n"
"access the file. Notify Head of State\n");
exit(1);
}
/*
printf("enter record number wanted to see\n"
"record amount is %d\n", cnt);
int rec;
scanf("%d", &rec);
if ( rec <= cnt)
{
cnt = 0;
while (fread(&set_of_rec, sizeof(set_of_rec), 1, bfp) > 0)
{
if (set_of_rec[cnt].id == rec)
{
len = ftell(bfp);
printf("LEN1 = %d\n", len);
printf("Id, Name, Address, amount in account\n");
printf("\n%d %s %s %.2lf\n", set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,set_of_rec[cnt].acc_bal);
len = ftell(bfp);
printf("LEN2 = %d\n", len);
break;
}
cnt++;
}
}
* */
// size_t where_to_go;
int recs[5];
// get multi "random user selected" records
//fseek(bfp,0,SEEK_SET);
fseek(bfp,(1*sizeof(set_of_rec[0])),SEEK_SET);
printf("Enter amount of records wanted, and which ones\n"
"enter up to 5 records\n");
scanf("%d", &amount);
switch (amount)
{
case 1:
scanf("%d", &recs[0]);
break;
case 2:
scanf("%d%d",&recs[0],&recs[1]);
break;
case 3:
scanf("%d%d%d", &recs[0],&recs[1],&recs[2]);
break;
case 4:
scanf("%d%d%d%d", &recs[0],&recs[1],&recs[2],&recs[3]);
break;
case 5:
scanf("%d%d%d%d%d", &recs[0],&recs[1],&recs[2],&recs[3],&recs[4]);
break;
default:
break;
}
printf(" Id, Name, Address, amount in account\n");
for(int i = 0; i < amount; i++)
{
// printf("pos1 %ld\n",ftell(bfp) );
// size_t size = (1*sizeof(set_of_rec));
// printf("size: %zu\n", size);
// where_to_go = (recs[i] * (1*sizeof(set_of_rec)));
// printf("where_to_go %zu\n", where_to_go);
// fseek(bfp, where_to_go, SEEK_SET);
fseek(bfp, (recs[i] * sizeof(set_of_rec)), SEEK_SET);
// fseek(bfp, where_to_go, SEEK_SET);
// printf("pos2 %ld\n",ftell(bfp));
// printf("rec #%d\n",recs[i]);
// printf("i = %d\n",i);
fread(&set_of_rec, sizeof(set_of_rec),4, bfp);
printf("\n%d %s %s %.2lf\n", set_of_rec[recs[i]-1].id,set_of_rec[recs[i]-1].name,set_of_rec[recs[i]-1].address,set_of_rec[recs[i]-1].acc_bal);
//fseek(bfp,(1*sizeof(set_of_rec[0])),SEEK_SET); //reset it back to start
}
fclose(bfp);
return 0;
}