Here is the snippet of code
typedef struct
{
double testA;
double testB[500];
bool isProcessed;
} MYSTURCT;
I have a binary file which is written with multiple structs of type "myStruct".
Now, in another function, I m trying to read the file and update in the middle.
void test()
{
FILE* fp = fopen (testFile, "r+")
MYSTURCT* myPtr = malloc (sizeof (MYSTRUCT));
while ( fread (myPtr,sizeof(MYSTRUCT),1,fp) )
{
if (!myPtr->isProcessed)
{
//update some thing int he struct
myPtr->testA = 100.00;
fseek (fp, -sizeof(MYSTRUCT), SEEK_CUR);
fwrite (myPtr,sizeof(MYSTRUCT), 1,fp);
}
}
}
Once I find something unprocessed, I update the struct in the memory, then try to write the struct to the disk. (first by seeking the CURR - sizeof(struct)) position and then fwriting the struct to disk.
Whats happening in my application is after doing the fseek, my
fp->_ptr is getting messed up and it looses the track of position in my stream.
Is there anything wrong that I am doing here?