SWAT /

Catching Ctrl-C

Reading

Cycling

Backpacking

X-Wing

Infinity

Warhammer 40k

Boardgames

Hobby

LEGO

Food

Code



Nook

sidebar

Catching Ctrl-C

Catching Control-C in C/C++

Do something like the following:

#include <signal.h>

//------------------------------------------------------
bool g_rmtWrapDone = false;

void CatchIntSignal(int n) {
  g_rmtWrapDone = true;
  // end CatchIntSignal
}

//------------------------------------------------------
int main(int argc, char *argv[]) {
  signal(SIGINT, CatchIntSignal);

  while (!g_rmtWrapDone) {
    // Do something fancy...
  }

  // end main
}

Usually this is most useful when there are multiple threads going on, etc. Along that vein, you may also wish to look into atexit().

Note that you may also wish to use SIGQUIT, and it's worth glancing at this screed. Another example is here; note that it re-installs the default handler (SIG_DFL), to kill everything on the second hit.

Recent Changes (All) | Edit SideBar Page last modified on December 14, 2007, at 04:07 PM Edit Page | Page History
Powered by PmWiki