在 freopen() 之后是否需要 fclose() 这两个文件?
Do you need to fclose() both files after freopen()?
我正在编写一个代码,它获取一个文件并将其保存到另一个名称不同的文件中 - 但是,我不确定是否需要关闭这两个文件?
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);
}
来自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. [...]
因此,您无需明确关闭上一个流,一旦使用完毕,只需关闭最近的流即可。
我正在编写一个代码,它获取一个文件并将其保存到另一个名称不同的文件中 - 但是,我不确定是否需要关闭这两个文件?
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);
}
来自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. [...]
因此,您无需明确关闭上一个流,一旦使用完毕,只需关闭最近的流即可。