简体   繁体   中英

Do you need to fclose() both files after freopen()?

I'm writing a code which takes one file and saves it into another with a different name - however, I am not sure whether or not I need to fclose both files or not?

FILE *logfile = fopen("log.txt", "a+");
while(1) {
    char filename[500];
    char logline[512]; 
    char channel[512];

    //Do stuff

    sprintf(filename, "%s.log.txt", channel);
    freopen(filename, "a+", logfile);
    log_to_file(logline, logfile);
}

From the man page

The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. [...]

So, you don't need to close the previous stream explicitly, once done usage, just close the recent stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM