Thread: atexit() and function parameter's

  1. #1
    Registered User
    Join Date
    Feb 2018
    San Diego, CA

    atexit() and function parameter's

    Hey all....thanks for previous help...starting to do about 4 hrs C programming a day.

    question with this line (function prototype and function itself)
    void close_function(FILE *fp, char buffer);

    I am not sure how to pass FILE *fp & buffer to the close_function function. I understand buffer isn't a value, but i cannot tell what it is. It's not a char array, and does not return a value.

    // Write a function that opens a new temporary file with a specified mode.  All temporary files created by this function should automatically be closed and deleted when the program terminates. (Hint: Use the atexit() library function).
    #include <stdio.h>
    #include <stdlib.h>
    void close_function(FILE *fp, char buffer);
    void start_function(void);
    int main( void )
        return 0;
    void start_function(void)
        FILE *fp;
        char mode[4];
        char buffer[80];
        while (1)
            puts("\nEnter a mode (max 3 characters): ");
            if ( (fp = fopen(buffer, mode )) != NULL )
                printf("\nSuccessful opening %s in mode %s.\n", buffer, mode);
    void close_function(FILE *fp, char buffer)
        puts("Done, temp file closed and deleted.\n");

    Thanks all for help who puts in time and effort to help me learn this language.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I'd say that you shouldn't use atexit for this. Rather, in this case the function that called fopen should have responsibility to call fclose. Use atexit for cases when you really need to do something at program exit, rather than cleanup that is the usual course of things for a function that allocates resources.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    at_exit functions can't have parameters, so if they need access to some data then that data must be global.

    But if you really want a temporary file you should use tmpfile, which is safer in creating the file and also automatically deletes it when your program terminates normally.
    #include <stdio.h>
    #include <stdlib.h>
    char g_tmpname[L_tmpnam];
    void exit_func() {
    int main() {
        FILE *f = fopen(g_tmpname, "w+");
        if (!f) {perror("fopen"); return 1;}
        fprintf(f, "hello?\n");
        char word[100];
        fscanf(f, "%s", word);
        printf("%s\n", word);
        fclose(f); // close the file!
        printf("%s\n", g_tmpname);  // so you can check if the file was deleted
        return 0;
    Ordinary language is totally unsuited for expressing what physics really asserts.
    Only mathematics can say as little as the physicist means to say. - Bertrand Russell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atexit() question.
    By HelpfulPerson in forum C Programming
    Replies: 8
    Last Post: 08-07-2013, 06:31 AM
  2. atexit
    By lolguy in forum C Programming
    Replies: 8
    Last Post: 01-16-2009, 06:30 PM
  3. atexit function
    By strokebow in forum C Programming
    Replies: 2
    Last Post: 11-24-2006, 04:45 PM
  4. atexit not running?
    By ichijoji in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2005, 03:28 PM
  5. how to use atexit()??
    By spike232 in forum C Programming
    Replies: 1
    Last Post: 03-16-2002, 09:40 AM

Tags for this Thread