I think I found a bug in fseek in writable files. Once you fseek to the end of the file, subsequent fseeks (at least with SEEK_SET) ignore the offset and position to the beginning of the file. Unfortunately I haven't found a workaround yet, but this behaviour is necessary for random access files. Interestingly ftell returns the intended offset.
Is this a problem of the mbed implementation ?
Code to reproduce:
#include "mbed.h"
LocalFileSystem local("local");
int main()
{
FILE *fp;
char buff1[5] = "ABCD";
char buff2[5] = "WXYZ";
char buff3[3] = "01";
fp = fopen("/local/out.txt","w");
if(!fp) printf("Error open file\r\n");
fwrite(buff1,1,4,fp); // file is now "ABCD" ->OK
fseek(fp,0,SEEK_END); //seek to end
// same effect with: fseek(fp,4,SEEK_SET);
fwrite(buff2,1,4,fp); // file is now "ABCDWXYZ" ->OK
fseek(fp,3,SEEK_SET); // this fseek sets fp to 0 instead of 3 !
fwrite(buff3,1,2,fp); // file should now be "ABC01XYZ" but is "01"
fclose(fp);
return(0);
}
I think I found a bug in fseek in writable files. Once you fseek to the end of the file, subsequent fseeks (at least with SEEK_SET) ignore the offset and position to the beginning of the file. Unfortunately I haven't found a workaround yet, but this behaviour is necessary for random access files. Interestingly ftell returns the intended offset.
Is this a problem of the mbed implementation ?
Code to reproduce:
#include "mbed.h"
LocalFileSystem local("local");
int main()
{
FILE *fp;
char buff1[5] = "ABCD";
char buff2[5] = "WXYZ";
char buff3[3] = "01";
fp = fopen("/local/out.txt","w");
if(!fp) printf("Error open file\r\n");
fwrite(buff1,1,4,fp); // file is now "ABCD" ->OK
fseek(fp,0,SEEK_END); //seek to end
// same effect with: fseek(fp,4,SEEK_SET);
fwrite(buff2,1,4,fp); // file is now "ABCDWXYZ" ->OK
fseek(fp,3,SEEK_SET); // this fseek sets fp to 0 instead of 3 !
fwrite(buff3,1,2,fp); // file should now be "ABC01XYZ" but is "01"
fclose(fp);
return(0);
}