🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to recover file destroyed by a failed fwrite call?

Started by
7 comments, last by PolarWolf 3 years, 8 months ago

I modified a very important file by fwrite, but an exception occurred during the call and the file size became 0. I think the data is still on disk because nothing was written. Is it possible to recover the information? The data inside are a product of days of work so any advice is appreciated.

Advertisement

Don't waste your time on chasing after lost data, and focus on improving your backup strategy so this never happens again. Seriously, several days of work and no backup? How does that even happen?

Yes I have given up and now modifying a most recent backup,it's a painful lesson,thanks.

fflush after each fwrite?

if you managed to just open seek rewrite then yeah try to open read write to another file (copy) then acutally open it

No fflush only a fclose after all writing,I tried to read the file use fread but the reading lenght is 0 because the file size is 0,I have made it up after hours of hard work,thanks

www.easeus.com

It recovered a whole drive for me years ago. Learnt my lesson, now I backup regularly ?

Money well spent.

PolarWolf said:

I modified a very important file by fwrite, but an exception occurred during the call and the file size became 0. I think the data is still on disk because nothing was written. Is it possible to recover the information? The data inside are a product of days of work so any advice is appreciated.

Not a recovery strategy, but to help avoid in future to a fairly high degree of confidence, especially when a file will need many write calls is you can create a temp file in the destination directory, write your file fresh, flush it, then rename over the original. Apart from some particular bad luck you should be left with one file or another intact in a failure situation.

Some programs will also autosave their documents to some temp location, and if on next start they see one of these temp files still exists offer to recover it.

Things like databases that modify very large files in-place I believe use other strategies like transaction logs or journals to help ensure they can recover to either the old or new state.

Recovery on some filesystems might be possible due to the order unused sectors are overwritten (especially if immediately ceased using the device), but probably in the realms of data recovery experts.

This reminds me some software's backup strategy,such as visual studio and photoshop,an excellent sollution,I will imply it in my tools.thanks a lot.

Do you use source control? If not, consider it for all important files. I use GitHub (and GitHub desktop) with a private repository, and it is so easy these days, that it would be almost criminal not to. There are many other similar solutions, with or without automatic cloud storage.

Good luck!

Geoff

@gdunbar A good advice,I never considered it, will check GitHub out,thanks.

This topic is closed to new replies.

Advertisement