I am writing data to a file, however on sudden power offs I can see garbage data in the file sometimes.
Sometimes the data is missing, sometimes it is editted and sometimes its a garbage data just before my first entry to the file on system reboot.
Is there a way I can make sure fwrite is consistent?
Just to add I am using “a” to append data to the file everytime.
Further, the garbage values or indermined data which I am seeing in the file, is it really due to the fwrite() operation?
FAT32 is not a journaling filesystem, so there is no way to guarantee consistent writes. Even if you flush the disk buffers after each write, the data could still be corrupted if there is a power loss just as the data is written to disk.
To answer your other question, no, the garbage isn’t caused by
fwriteat all, it is because the filesystem has not completed the write operation in its entirety.