I'm having trouble writing the folowing structure to a binary file:
Code:
typedef struct
{
int day, month, year;
} Aval;
typedef struct
{
char name[100], state[50];
int freq, work;
float *n_aval, final_note;
Aval *n_data;
}CURSO;
The program writes everything fine except the data of Aval type.
This is the function I am using just to get the days, month and year to an array:
Code:
void Date_Eval(CURSO v[])
{
char aux[]="SIGNED", name[100], buff[256];
int i, pos, j;
fgets(buff,255,stdin);
printf("\n");
printf("LIST:\n");
printf("\n");
for(i=0;i<28;i++)
{
if((strcmp(aux,v[i].state))==0) //print the disciplines that i signed in
printf("%s\n", v[i].name);
}
gets(name);
for(i=0;i<28;i++)
{
if((strcmp(name,v[i].name))==0) //store the position that the discipline is in, in the array
pos=i;
}
if((v[pos].n_aval=(float*)realloc(v[pos].n_aval,sizeof(float)*(v[pos].freq+v[pos].work)))==NULL) //v[pos].freq+v[pos].work is the size of the array.
{
printf("ERRO\n");
}
else
{
if((v[pos].n_data=(Aval *)realloc(v[pos].n_data,sizeof(Aval)*(v[pos].freq+v[pos].work)))==NULL) //v[pos].freq+v[pos].work is the size of the array.
{
printf("ERROR\n");
}
else
{
for(j=0;j<(v[pos].freq+v[pos].work);j++)
{
printf("Day of evalutation %d: ", j+1);
scanf("%d",&v[pos].n_data[j].day);
printf("Month of evaluation %d: ", j+1);
scanf("%d",&v[pos].n_data[j].month);
printf("Year of evaluation %d: ", j+1);
scanf("%d",&v[pos].n_data[j].year);
}
}
}
}
The data is stored correctly in memory, because I can see it while in the program, but when I exit and enter again it's no longer there.
I am reading in the begining:
Code:
if((f=fopen("Data.dat","rb"))==NULL)
{
printf("ERROR\n");
}
else
{
fread(v,sizeof(CURSO),28,f); //--> 28 is the number of discipline i want to read
........................
fclose(f);
And only writing in the end:
Code:
if((f=fopen("Data.dat","wb"))==NULL)
{
printf("ERROR\n");
}
else
{
fwrite(v,sizeof(CURSO),28,f); //-->28 is the number is the number of disciplines i want to write.
}
fclose(f);
I have tried opening and and writing in every function but the result is the same.
If anyone could help...And sorry for my bad english.
Thanks in advance.