I need write a byte in binary file, but only if this is diferent (something looked like EEPROM.update(a) from Arduino IDE). I think there is some function that does this, but now I can't find it/I'm not sure that exists.
So far I have do this:
FILE *fp = fopen("file.dat", "r+");
fseek(fp, (long) address, SEEK_SET);
fread(&value, sizeof(uint8_t), 1, fp);
if (value == val) {
fclose(fp);
exit();
}
fseek(fp, (long) address, SEEK_SET);
fwrite(v, sizeof(uint8_t), 1, fp);
fclose(fp);
This code is an example, not the real code.
Thanks for your time.
The fread
advances the file pointer, so your fwrite
doesn't overwrite the byte at offset address
, but rather the following byte. You need to fseek
back to offset address
again before writing.
Also there is a v
in your sample code which isn't defined. Probably it should be &val
or something like that.