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?