Is it possible (on iPhone/iPod Touch) for a file written like this:
if (FILE* file = fopen(filename, "wb")) {
fwrite(buf, buf_size, 1, file);
fclose(file);
}
to get corrupted, e.g. when app is forced to terminate?
From what I know fwrite should be an atomic operation, so when I write whole file with one instruction no corruption should occure. I could not find any information on the net that would say otherwise.
Since when
fwriteis atomic? I can’t find any references. Anyway, even iffwritecan be atomic, the time betweenfopenandfwriteis not, so if your app is forced to terminate between those times, you’ll get an empty file.As you’re writing for iPhoneOS, you can use
-[NSData writeToFile:atomically:]to ensure the whole open-write-close procedure is atomic (it works by writing to a temporary file, then replace the original one.)