/*testfile: a file to work out bugs in storing structures*/
/*2/14/92*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
FILE *fptr;
typedef struct{
char last_pay_date[12];
double last_pay;
double pay_to_intr;
double pay_to_princ;
double loan_bal;
double penalty;
char next_pay_date[12];
}PayRecord;
PayRecord pay={"",0,0,0,0,0,""};
double round_struct(double x);
main()
{
char payfile[13]="cathie.pdf";
double x,z;
long sop,curpos;
char inbuf[4];
clrscr();
strcpy(pay.last_pay_date,"1/1/1992");
strcpy(pay.next_pay_date,"2/1/1992');
pay.loan_bal=10000;
sop=sizeof(pay);
x=1625;
z=x/12;printf("input 1625/12 gives these results\n");
/*z=x/10;printf("input 1625/12 gives these results\n");*/
pay.last_pay=round_struct(z);
if((fptr = fopen(payfile,"w")) == NULL)
{
printf("could not open file\n");getch();
}
fwrite (&pay,sizeof(pay),1,fptr);
fseek(fptr,OL,SEEK_END);
curpos=ftell(fptr);
printf("untruncated input=%1f\n",z);
printf("truncated input=%1f\n",pay.last_pay);
printf('sizeof(pay)=%ld EOF pointer=%1d \n",sop,curpos);
fclose(fptr);
getch();
return(0)
}
double round_struct(double x)
{
double y,z;
y=x*100;
z=ceil(y);
return (z/100);
}
Output:
input gives these results
untruncated input=135.416667
truncated input=135.420000
sizeof(pay)=64 EOF pointer=65
input 1625/10 gives these results
untruncated input=162.500000
truncated input=162.500000
sizeof(pay)=64 EOF pointer=64
/* End of File */