I have some code that writes to a file, if the file already exists it needs to update bytes 5-8. This is not happening tho, so I added some printfs to try and figure out whats going wrong:
Code:
int PolygonSave(Polygon p, const char* filename, int append)
{
FILE *file = fopen(filename, (append)? "a+b": "w+b");
int num_poly = 1;;
if(ftell(file) < 9) //Write header info
{
fwrite("POLY", 1, 4, file);
fwrite(&num_poly, sizeof(int), 1, file);
}
else // Read the number of polygons, increment & write back
{
fseek(file, 4, SEEK_SET);
fread(&num_poly, sizeof(int), 1, file);
num_poly++;
fseek(file, 4, SEEK_SET);
fwrite(&num_poly, sizeof(int), 1, file); // This should overwrite - hopefully
fseek(file, 0, SEEK_END);
printf("ISDOINGSOMETHING\n");
}
printf("FS: %u\n", ftell(file));
printf("NP: %u\n", num_poly);
Heres what I dont get: If the file exists then ftell gives the correct size, which will be >8. This means that the code in the else block should be executed; however, it never prints ISDOINGSOMETHING. I'm getting no errors or warnings with gcc -Wall -std=c99