Code:
#include <stdio.h>
#include <math.h>
#define N 100 // number of points
#define TRUE 1
#define FALSE 0
#define EPSILON 1e-10
#define BINFILE "file.bin"
typedef struct LITTEINFO
{
double l; // starting time
double c; // ending time
double v; // velocity
double td; // time
double pc; // altitude
} INFO;
typedef struct LITTEINFO2
{
double l; // starting time
double c; // ending time
double v; // velocity
double td; // time
double pc; // altitude
} INFO2;
int main()
{
INFO data;
int i=0;
int flag = FALSE;
int ans;
FILE *fp;
if ((fp = fopen(BINFILE, "wb+")) == NULL)
fp = fopen(BINFILE, "wb+");
do
{
printf("Please enter the value for L:\n");
scanf("%lf", &data.l);
printf("Please enter the value for C:\n");
scanf("%lf", &data.c);
printf("Please enter the value for the battery voltage:\n");
scanf("%lf", &data.v);
printf("Please enter the value for the dissipation time td:\n ");
scanf("%lf", &data.td);
printf("Please enter the value for the percentage of the ");
printf("\n original charge pc to reach within td\n");
scanf("%lf", &data.pc);
fwrite(&data, sizeof(data), 1 ,fp);
i++;
} while (i < 2);
fclose(fp);
if ((fp = fopen(BINFILE, "rb")) == NULL)
{
printf("NULL\n");
return -1;
}
while (fread(&data, sizeof(data), 1, fp) > 0)
printf("\n%lf %lf %lf %lf %lf", data.l, data.c, data.v, data.td, data.pc);
fclose(fp);
INFO2 data2[5];
// int i=0;
//int flag = FALSE;
//int ans;
FILE *fp2;
i=0;
while(i < 5 && flag==FALSE)
{
printf("\n\nPlease 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 ");
printf("\n original charge pc to reach within td\n");
scanf("%lf",&data2[i].pc);
i = i+1;
printf("do you want to 1. enter a new set of values(max 5) and save these values\n");
printf("or 2.choose a saved set of values\n");
scanf("%d",&ans);
if(ans==1)
{
if (( fp2 = fopen(BINFILE, "wb+")) == NULL)
fp2 = fopen(BINFILE, "wb+");
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);
fclose(fp2);
}
else
{ flag = TRUE;
if ((fp2 = fopen(BINFILE, "rb")) == NULL)
{
printf("NULL\n");
return -1;
}
int e = 0;
while (fread(&data, sizeof(double), 1, fp2) > 0) {
printf("\n%lf %lf %lf %lf %lf", data2[e].l, data2[e].c, data2[e].v, data2[e].td, data2[e].pc);
e++;
}
/*
fread(&data[0].l,sizeof(data),1,fp);
fread(&data[0].c, sizeof(data), 1, fp);
fread(&data[0].v, sizeof(data), 1, fp);
fread(&data[0].td, sizeof(data), 1, fp);
fread(&data[0].pc, sizeof(data), 1, fp);
*/
fclose(fp2);
}
}
return 0;
}
input/output