Thread: atexit() and function parameter's

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

    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)
    Code:
    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.

    Code:
    // 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 )
    {
        atexit(close_function);
    
        start_function();
    
        return 0;
    }
    
    void start_function(void)
    {
        FILE *fp;
    
        char mode[4];
        char buffer[80];
    
        tmpnam(buffer);
    
        while (1)
        {
    
            puts("\nEnter a mode (max 3 characters): ");
            gets(mode);
    
            if ( (fp = fopen(buffer, mode )) != NULL )
            {
                printf("\nSuccessful opening %s in mode %s.\n", buffer, mode);
            }
    
            break;
        }
    }
    
    void close_function(FILE *fp, char buffer)
    {
        fclose(fp);
        remove(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
    Location
    Singapore
    Posts
    28,401
    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
    Posts
    1,664
    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.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    char g_tmpname[L_tmpnam];
     
    void exit_func() {
        remove(g_tmpname);
    }
     
    int main() {
        atexit(exit_func);
     
        tmpnam(g_tmpname);
     
        FILE *f = fopen(g_tmpname, "w+");
        if (!f) {perror("fopen"); return 1;}
        fprintf(f, "hello?\n");
        rewind(f);
        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;
    }
    All truths are half-truths. - A.N. Whitehead

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