Thread: How do I do something when a program exits?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    How do I do something when a program exits?

    Hi

    I'm running a program that is a continuous loop. I want to dump an array into a text docuemnt when I press ctrl-c to exit. I can already put the arrary into the text doc, I just want to do it when the program exits now.

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    man atexit
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'm not sure about whether Ctrl-C results in "normal program termination." So something like this might also be of interest.
    Code:
    #include <stdio.h>
    #include <signal.h>
    
    volatile sig_atomic_t Done = 0;
    
    void handler(int code)
    {
       Done = 1;
    }
    
    int main(void)
    {
       signal(SIGINT, handler);
       puts("Entering Loop");
       while ( !Done )
       {
          /* ... */
       }
       puts("Saving File");
       return 0;
    }
    Last edited by Dave_Sinkula; 05-25-2005 at 12:19 PM. Reason: Added 'volatile'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Call a program & still capture cmds
    By Zuconta in forum C Programming
    Replies: 3
    Last Post: 07-23-2003, 03:54 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM