-
Problems - writing File
Code:
void add_seating(struct_seating seating_temp)
{
FILE *fp_index, *fp_seating;
int i = 0;
int j =0;
int cont =1;
int continue_index = 1;
indexS index_temp[FILE_SIZE];
//struct_seating seating_temp;
fp_index = fopen("array_seating_index.bin", "r+b");
if(fp_index == NULL)
{
fprintf(stderr, "Error, File: array_seating_index.bin");
}
fp_seating = fopen("array_seating.bin", "r+b");
if(fp_seating == NULL)
{
fprintf(stderr, "Error, File: array_seating.bin");
}
fread(&index_temp[0],sizeof(indexS),1,fp_index);
while(cont ==1)
{
j=0;
fseek(fp_seating, i*sizeof(struct_seating),SEEK_SET);
printf("Value is: %d\n", seating_temp.sectionA[0]); //prints correct value of sectionA[0]
fread(&seating_temp, sizeof(struct_seating),1,fp_seating);
printf("Value is, before if: %d\n", seating_temp.sectionA[0]); //Doesnt print correct value of sectionA[0]
if(seating_temp.event_num == 0)
{
printf("Value is: %d\n", seating_temp.sectionA[0]);
fseek(fp_seating, i*sizeof(struct_seating),SEEK_SET);
fwrite(&seating_temp, sizeof(struct_seating),1,fp_seating);
while(continue_index ==1)
{
if(index_temp[j].index_event_num == 0)
{
//printf("equal to 0\n");
index_temp[j].index_event_num = seating_temp.event_num;
index_temp[j].index_seating = i;
fseek(fp_index, 0*sizeof(indexS),SEEK_SET);
fwrite(&index_temp[0],sizeof(indexS),1,fp_index);
continue_index = 0;
}
else
if(j == FILE_SIZE)
{
printf("File ***** full!\n");
printf("j is: %d\n");
continue_index = 0;
}
j++;
}
cont = 0;
}
else
if(i == FILE_SIZE - 1)
{
fprintf(stderr, "File full!!!\n");
cont = 0;
}
else
i++;
}
fclose(fp_seating);
fclose(fp_index);
}
There are two comments there that show the value of sectionA[0] are changed. If I take out he fread, where the comments are, the value remains okay, however I never have anything written to the file, and I dont know why.
Anyone?
Thanks.
-
After fixing several functions, I have a few more questions, and a little puzzled.
I pass the fucntion a struct, when I perform the first fread, is it not reading from the stream, which is a file?
If so, my struct is never read, but what is in the file is.
Can someone explain this for me?
Thanks again.
-
After looking at the code, I realized the problem.
I was writing a dummy record to the file, instead of the one passed as a param.:rolleyes: