Code:
#include <stdio.h>
#include <math.h>
#define N 100 // number of points
#define TRUE 1
#define FALSE 0
#define EPSILON 1e-30
#define BINFILE "file.bin"
#define pi 3.1415926535897932384626433832795
typedef struct
{
double l; // starting time
double c; // ending time
double v; // velocity
double td; // time
double pc; // altitude
} INFO;
// Function Prototypes
void findRoot(INFO[], int , double * );
void plotFunc(double, double, int, double []);
double func(INFO [],int , double);
void plot(int, double[], double[], int, double[], double[]);
double getMin(double *, int );
double getMax(double *, int );
int main()
{
int i=0;
int flag = FALSE;
int flag2;
int ans;
INFO data2[5];
FILE *fp2;
double root , R;
double check , check2;
double f;
char ans2;
if (( fp2 = fopen(BINFILE, "wb")) != NULL )
{
printf("Do you want to choose previously inputted values? (y/n)\n");
fflush(stdin);
scanf("%c",&ans2);
if (ans2 == 'y')
flag = TRUE;
fp2 = fopen(BINFILE, "wb");
}
while(i < 5 && flag==FALSE)
{
do
{
flag2 = TRUE;
printf("Please enter the value for L:\n");
scanf("%lf",&data2[i].l);
printf("Please enter the value for C:\n");
scanf("%lf",&data2[i].c);
printf("Please enter the value for the battery voltage:\n");
scanf("%lf",&data2[i].v);
printf("Please enter the value for the dissipation time td:\n ");
scanf("%lf",&data2[i].td);
printf("Please enter the value for the percentage of the\n"
"original charge pc to reach within td\n");
scanf("%lf",&data2[i].pc);
R = (-2* data2[i].l * log(data2[i].pc))/data2[i].td;
check = 1/(data2[i].l*data2[i].c);
check = check - pow((R/(2*data2[i].l)),2) ;
check2 = sqrt(check);
f = check2/(2*pi);
f= 50/f;
if(check < 0)
{
flag2 = FALSE;
printf("Invalid input , Please enter a bigger value for td\n");
}
if(f < data2[i].td)
{
flag2 = FALSE;
printf("Frequency too high; plot may be distorted\n");
}
}while(flag2 == FALSE);
fwrite(&data2[i].l, sizeof(double), 1, fp2);
fwrite(&data2[i].c, sizeof(double), 1, fp2);
fwrite(&data2[i].v, sizeof(double), 1, fp2);
fwrite(&data2[i].td, sizeof(double), 1, fp2);
fwrite(&data2[i].pc, sizeof(double), 1, fp2);
//fwrite(&data2, sizeof(data2), 1 ,fp2);
i++;
printf("do you want to \n"
"1. enter a new set of values(max 5)\n"
"or\n"
"2.choose a saved set of values\n");
scanf("%d",&ans);
if (ans == 2)
flag = TRUE;
}
printf("which set of values do you want to use?\n");
scanf("%d",&ans);
fclose(fp2);
fp2 = fopen(BINFILE, "rb");
// compensate for zero element.
if ( ans-1 > 6)
{
printf("out of bounds\n");
return -1;
}
else
{
fread(&data2[ans-1].l, sizeof(double), 1, fp2);
fread(&data2[ans-1].c, sizeof(double), 1, fp2);
fread(&data2[ans-1].v, sizeof(double), 1, fp2);
fread(&data2[ans-1].td, sizeof(double), 1, fp2);
fread(&data2[ans-1].pc, sizeof(double), 1, fp2);
}
printf("%lf\n",data2[ans-1].l);
findRoot(data2,ans,&root);
fclose(fp2);
return 0;
}
void findRoot(INFO data2[] , int sel , double *root)
{
sel = sel -1;
double upper = sqrt(4*data2[sel].l/data2[sel].c) ;
double lower = 0;
double x0 , x1 ;
double sol , sol1;
x0=upper;
x1= lower;
sol = func(data2,sel,x0);
sol1 = func(data2,sel,x1);
if(func(data2,sel,x0)* func(data2,sel,x1) < 0)
{
do
{
*root = (x0+x1)/2;
if(func(data2,sel,*root)*func(data2,sel,x1) < 0 )
x0 = *root;
else x1 = *root;
}while(fabs(func(data2,sel,*root)*func(data2,sel,x1)) > EPSILON);
}
}
double func(INFO data[] ,int sel , double x)
{
double root;
root = exp(-(x*data[sel].td)/(2*data[sel].l));
root = root - data[sel].pc ;
return(root);
}
when i run the program and created a bin file , then close it and try to read from it again , between closing the program and restarting it something seems to go wrong and also Thanks I have checked post 10 , the file is present in this case.