   Randomly corrupted files (C++)
  Hello everyone.I'm working on a terrain editor that uses streaming both while editing and while viewing the terrain.However, there's some strange behaviour happening randomly: If i have say, 24 chunks (which make 24 files), and if i store the terrain into those files constantly when saving/loading the chunks as needed, sometimes a chunk file will have 0 bytes, and give no error or anything. Neither fwrite nor fflush (yes, using stdio.h) give any errors either, which is very strange. Needless to say, the program has issues due to not being able to read back the data.I'm using C++, and this already happened on various OSes (read: windows XP, Vista, and 7). Does anyone have any idea of a possible cause to this? This usually happens if i save/load files quickly.Any information would be helpful.Thank you for your time regarding this message,Nuno Silva.   
  Are you closing the file with fclose?Does it return an error?It sounds like you are trying to write to the file while its still being used or  being written to.   ;Quote:Original post by Black KnightAre you closing the file with fclose?Does it return an error?It sounds like you are trying to write to the file while its still being used or  being written to.Yes, i'm closing the file, i'm sure of that.   ;  You're not doing anything fun with multiple threads, are you?   ;Quote:Original post by NypyrenYou're not doing anything fun with multiple threads, are you?Don't think so, on Edit Mode (aka the setting where "shit happens"), there're no threads other than the main thread. On View Mode, though, it has a loader thread.   ;  Not to be pedantic, but in C++ you should not be using stdio.h routines - you should be using filestreams from fstream. They are type safe, much harder to break into security-wise, and more reliable in the face of corrupted/invalid data. There's also the fact that file access via fstream is fully RAII, meaning it is safe for exceptions and doesn't require manual hand-holding to do things like close and flush the file.We can speculate a bit on what the actual problem is, but it'd be very useful if you could provide the code for saving files so we can look it over.   ;Quote:Original post by ApochPiQWe can speculate a bit on what the actual problem is, but it'd be very useful if you could provide the code for saving files so we can look it over.I'll try rewriting my file layer under fstream then. I'd show the source, but it's quite large... Not fit for this forum. I'll report any news ASAP.   ;  Well, i rewrote my file layer under fstream, and i still have the same problem. I wonder what can be the issue...Since fstream is quite hard to break, it's probably a bug in my code, i guess. Obscure bug hunt time.The only time this seems to happen is when i store/restore the terrain chunks into a terrain package. I sequentially open each chunk, read data, store their data into the terrain package file, and destroy the file stream object.Thanks for your help, everyone.   ;  Sorry for triple posting...Just figured out what was wrong. Hahaha, can't believe i did this...While storing the terrain package, i was serializing all chunks into each of the files.However, i didnt notice that the code i do to check if the chunk is loaded is done after opening the file for writing...Therefore, if a chunk wasnt loaded, the file would have it's data corrupted either way...Sorry for all this trouble, and thanks a lot for your help, everyone!   