freopen
场景:
1.比如控制台打打印太长了,需要记录到文件上分析。
2.或者是做一些简单的日志工作就可以用到freopen。
代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <io.h>
#include <assert.h>
using namespace std;
char *GetFileDirectory(const char *filename) {
char *ret = NULL;
char dir[1024];
char *cur;
if (filename == NULL) return(NULL);
#if defined(WIN32) && !defined(__CYGWIN__)
# define IS_SEP(ch) ((ch=='/')||(ch=='\\'))
#else
# define IS_SEP(ch) (ch=='/')
#endif
strncpy(dir, filename, 1023);
dir[1023] = 0;
cur = &dir[strlen(dir)];

本文介绍了如何在C/C++中利用freopen函数将标准输出重定向到文件,适用于记录长篇控制台输出或简单日志记录。示例代码展示了如何打开.log.txt文件进行重定向。
订阅专栏 解锁全文
2536

被折叠的 条评论
为什么被折叠?



