2

I have a program that will create a simple txt file in the same directory as the compiled .exe:

ofstream output("myfile.txt", ios::binary | ios::trunc);

At the end of my program, I have this to remove it:

remove("myfile.txt");

Both of these work well, however, I want the file deleted if the user closes the cmd window unexpectedly, accidently, or they end the process.

Sam
  • 7,252
  • 16
  • 46
  • 65
mrg95
  • 2,371
  • 11
  • 46
  • 89
  • Might be worth starting a different process to watch for this one to end. – chris Jun 01 '13 at 04:49
  • Interesting... can you elaborate or give a quick example? :) – mrg95 Jun 01 '13 at 04:50
  • 1
    On which operating system? Can't you make that output file a temporary file (e.g. with [tmpfile(3)](http://man7.org/linux/man-pages/man3/tmpfile.3.html), at least on Linux, MacOSX and other POSIX systems) ? – Basile Starynkevitch Jun 01 '13 at 04:52
  • Window 7. I don't think so considering I have a completely seperate exe that uses this txt also. I want it written to the users system, then deleted when the program closes (maybe catch it when it crashes???) – mrg95 Jun 01 '13 at 04:54
  • Well, creation is easy enough with `WinExec`, `ShellExecute`, or `CreateProcess`. From what I can tell, it's not exactly trivial to make a robust watcher for crashes. Of course there are worse methods that are much easier, like checking whether it's still alive every few seconds. – chris Jun 01 '13 at 05:00
  • your answer seems to be here: http://stackoverflow.com/questions/390615/finally-in-c – atk Jun 01 '13 at 05:01
  • @atk, Indeed, though crashes seem to be a necessary consideration as well, and when crashes are involved, a separate process is typically a good idea. – chris Jun 01 '13 at 05:03
  • @chris: I imagine an exception handler would also be an important consideration. – atk Jun 01 '13 at 05:04
  • @atk, Definitely, though the separate process would handle both. It comes down to choosing how picky you want to be and then only implementing it once :) – chris Jun 01 '13 at 05:05
  • @user2356609, Wait, do you want this file deleted every time the process ends or only unexpectedly? – chris Jun 01 '13 at 05:06

1 Answers1

4

The standard way to clean up your process is to register a function with atexit.

void clean_myfile {
    std::remove( "myfile.txt" );
}

int main() {
    std::ofstream output("myfile.txt", std::ios::binary | std::ios::trunc);
    std::atexit( clean_myfile );
}

This will run if the process is exited gracefully, platform details notwithstanding.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • This works great! I had to add () after the function declaration but it works when I close the window :) – mrg95 Jun 01 '13 at 05:27
  • I wish more people on SO gave simple answers like this :) – mrg95 Jun 01 '13 at 05:28
  • 2
    Notice that the `atexit` registered handler `clean_myfile` won't run if your program is killed, e.g. because of some segmentation violation.... As Potatoswatter answered, it will only run if the process is exiting *gracefully*.... – Basile Starynkevitch Jun 01 '13 at 06:02