ofstream corrupting file when program terminated

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 8
Reputation: cog_bn is an unknown quantity at this point 
Solved Threads: 0
cog_bn cog_bn is offline Offline
Newbie Poster

ofstream corrupting file when program terminated

 
0
  #1
Jul 26th, 2009
Hi,

Some time ago, I wrote a program that used the write() function of an fstream object to write a large file to a 100MB-zip-disk (it was an old computer). When I "End Now"ed the program I found that the zip disk was corrupted. The file didn't show up but a lot of the free space was gone, and I had to format the disk.

I'm guessing it was because the close() function was never called, and so the result would have been the same if I had run the program on my hard drive (which I'm glad I didn't). Is there any way of getting around this problem?

I know there's an atexit() function, but I tried it with cout and it crashed.

Thanks for your time
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: ofstream corrupting file when program terminated

 
0
  #2
Jul 26th, 2009
Please post down your program's code, I'd be glad if it could corrupt files on my harddrive
Last edited by tux4life; Jul 26th, 2009 at 2:49 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: ofstream corrupting file when program terminated

 
0
  #3
Jul 26th, 2009
Even if your program doesn't "close" the file, the run-time library will, and failing that the OS will do it when the process terminates.

On a hard disk, the worst that could ever happen would be lost sectors. NTFS is pretty robust against such things (extreme case is power loss, and disks routinely pass this test without severe damage).

ZIP-drives were not exactly the most reliable technology as I recall, so it could just as well have been buggy drivers as your code.

If you're just using the standard library I/O mechanism, there are a lot of steps between your code and the surface of the disk. Any of which could go wrong in a spectacular fashion.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: cog_bn is an unknown quantity at this point 
Solved Threads: 0
cog_bn cog_bn is offline Offline
Newbie Poster

Re: ofstream corrupting file when program terminated

 
0
  #4
Jul 27th, 2009
I'm really sorry about this: I have no idea what's going on now. I've ran the same program and it's produced a different result.

Last time, the file that was created didn't even show up until the program exited (just after when the close() function was called), and that's why when lots of disk space was taken up but no file produced anywhere I blamed the close() function not being called. And I did search for that file, pressed F5, everything.

This time, the file was produced right at the start and it's size just kept increasing. Plus, when I tried to close the program it just closed without windows saying "this program is not responding. do you want to End Now". I've been trying the whole morning and I can't get it to crash like last time.

If anyone knows what's going on that'll be brilliant, but I think it was probably just the computer misbehaving. Or maybe windows updated itself at some point.

PS

The code was just a normal
  1. ...
  2. fstream file("rubbish.bin",ios::out|ios::binary|ios::app);
  3. for(/*some random condition*/){
  4. file.write(/*some random data*/);
  5. }
  6. ...

And I wondered whether it was the "ios::app" that did something funny, but it's not that either.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: ofstream corrupting file when program terminated

 
0
  #5
Jul 27th, 2009
>The code was just a normal......
Would you mind posting the whole program please?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: ofstream corrupting file when program terminated

 
0
  #6
Jul 27th, 2009
Well yes, if for(/*some random condition*/) never terminates, then your disk will fill up rapidly.

Post actual code, not a summary (or even an abridged or abbreviated version).
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: cog_bn is an unknown quantity at this point 
Solved Threads: 0
cog_bn cog_bn is offline Offline
Newbie Poster

Re: ofstream corrupting file when program terminated

 
0
  #7
Jul 27th, 2009
Fine, if you insist:

  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void main()
  7. {
  8. char* data=new char[10<<20];
  9. unsigned int size;
  10. cout<<"Size (in MB, to the nearest 10):\n\t";
  11. cin>>size;
  12. size=size/10;
  13. cin.ignore(0x7fffffff,'\n');
  14. cout<<"\n\nData written (MBs):\t 0..."; //4 spaces
  15. fstream file("RandomRubbish.bin",ios::out|ios::binary);
  16.  
  17. for(int MB=1;MB<=size;MB++)
  18. {
  19. for(int i=0;i<(10<<20)-8;i+=8){
  20. (*reinterpret_cast<unsigned long long int*>(data+i))++;
  21. }
  22. file.write(data,10<<20);
  23. if(file.bad()){
  24. cout<<"\a\a\a\a\a\n\nFile bad!!\n\n";
  25. for(int i=0;i<20;i++)cout<<'\a'; //To get my attention
  26. cin.get();
  27. return;
  28. }
  29.  
  30. cout<<"\b\b\b\b\b\b\b\b"<<setw(4)<<MB<<"0...";
  31. }
  32.  
  33. cout<<"\n\nDone.\a\n\n";
  34. cin.get();
  35. }

As I say though, it's working fine now. And I've tried "End Process Tree"ing it, it was still working fine. It was probably just the computer misbehaving back then. Maybe there was some other program (I have changed the anti-virus from F-Secure to Sophos - don't know if that could have affected it) or something. Or maybe it was just an out-of-date zip drive driver.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC