赞
踩
和别人讨论问题,说到IO重定向。只有在低版本vs中, 替换_iob[]的方法。
win版,可以用freopen来实现IO重定向.
linux版,可以用dup2来实现IO重定向.
如果能使用API来进行IO重定向,那兼容性好多了. 代码实现也优雅的多:)
// @file io_redirect.cpp
// @brief io redirect to file, env is vs2017
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#ifndef MAXBYTE
#define MAXBYTE 0xff
#endif // #ifndef MAXBYTE
#define LOG_FILE_PATH_NAME "d:\\my_log.txt"
FILE* g_fp_log_file = NULL;
FILE* g_fp_stdout_org = NULL;
FILE* g_fp_stdout_new = NULL;
void log_init();
void log_uninit();
int main_proc(int argc, char** argv);
void show_file_content(const char* psz_file_path_name);
int main(int argc, char** argv)
{
printf(">> main\r\n");
system("del d:\\my_log.txt");
log_init();
main_proc(argc, argv); // this print on file
log_uninit();
// this print on console
printf(">> ---------- d:\\my_log.txt ----------\r\n");
show_file_content(LOG_FILE_PATH_NAME);
printf("<< ---------- d:\\my_log.txt ----------\r\n");
printf("<< main\r\n");
system("pause");
/* run result on consle
>> main
>> log_init
<< log_uninit
>> ---------- d:\my_log.txt ----------
<< log_init
argc = 1
argv[0] = D:\ls_RE\prj\study\linux_prj\printf_redirect\win\x64\Debug\printf_redirect.exe
>> log_uninit
<< ---------- d:\my_log.txt ----------
<< main
请按任意键继续. . .
*/
return 0;
}
void log_init()
{
printf(">> log_init\r\n");
// 用替换_iob[]中的FILE*不通用,高版本vs没有_iob
// printf use stdout as io handle
// #define stdout (__acrt_iob_func(1))
// _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned _Ix);
/*
#ifndef _FILE_DEFINED
#define _FILE_DEFINED
typedef struct _iobuf
{
void* _Placeholder;
} FILE;
#endif
*/
// 用freopen兼容性很好
g_fp_stdout_org = stdout;
g_fp_log_file = freopen(LOG_FILE_PATH_NAME, "a+", stdout);
if (NULL == g_fp_log_file) {
printf("error : can't io redirect stdout\r\n");
}
printf("<< log_init\r\n");
}
void log_uninit()
{
printf(">> log_uninit\r\n");
if (NULL != g_fp_log_file) {
fflush(stdout);
fclose(g_fp_log_file);
g_fp_log_file = NULL;
g_fp_stdout_new = freopen("CON", "w", stdout);
if (NULL == g_fp_stdout_new) {
printf("error : can't io redirect stdout\r\n");
}
}
printf("<< log_uninit\r\n");
}
int main_proc(int argc, char** argv)
{
int i = 0;
printf("argc = %d\r\n", argc);
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\r\n", i, argv[i]);
}
return EXIT_SUCCESS;
}
void show_file_content(const char* psz_file_path_name)
{
FILE* fp = NULL;
char sz_buf[4096] = { '\0' };
do {
if (NULL == psz_file_path_name) {
break;
}
fp = fopen(psz_file_path_name, "r");
if (NULL == fp) {
break;
}
do {
memset(sz_buf, 0, sizeof(sz_buf));
if (NULL == fgets(sz_buf, (int)sizeof(sz_buf), fp)) {
break;
}
printf("%s", sz_buf);
} while (1);
} while (0);
if (NULL != fp) {
fclose(fp);
fp = NULL;
}
}
// @file io_redirect.cpp
// @brief io redirect to file, env is debian7.5
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifndef MAXBYTE
#define MAXBYTE 0xff
#endif // #ifndef MAXBYTE
#define LOG_FILE_PATH_NAME "/var/log/my_log.txt"
int g_fd_stdout_dup = -1;
FILE* g_fp_log_file = NULL;
void log_init();
void log_uninit();
int main_proc(int argc, char** argv);
void show
int main(int argc, char** argv)
{
printf(">> main\r\n");
system("rm /var/log/my_log.txt");
log_init();
main_proc(argc, argv); // this print on file
log_uninit();
// this print on console
printf(">> ---------- /var/log/my_log.txt ----------\r\n");
system("cat /var/log/my_log.txt");
printf("<< ---------- /var/log/my_log.txt ----------\r\n");
printf("<< main\r\n");
/* run result on consle
>> main
>> ---------- /var/log/my_log.txt ----------
argc = 1
argv[0] = ./case_io_redirect
<< ---------- /var/log/my_log.txt ----------
<< main
*/
return 0;
}
void log_init()
{
int fd_stdout = -1;
int fd_log_file = -1;
g_fp_log_file = fopen(LOG_FILE_PATH_NAME, "a+");
if (NULL != g_fp_log_file) {
if (-1 == g_fd_stdout_dup) {
g_fd_stdout_dup = dup(STDOUT_FILENO);
}
fd_log_file = fileno(g_fp_log_file);
fd_stdout = dup2(fd_log_file, STDOUT_FILENO);
}
}
void log_uninit()
{
if (NULL != g_fp_log_file) {
fflush(stdout);
fclose(g_fp_log_file);
g_fp_log_file = NULL;
}
if (-1 != g_fd_stdout_dup) {
dup2(g_fd_stdout_dup, STDOUT_FILENO);
close(g_fd_stdout_dup);
g_fd_stdout_dup = -1;
}
}
int main_proc(int argc, char** argv)
{
int i = 0;
printf("argc = %d\r\n", argc);
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\r\n", i, argv[i]);
}
return EXIT_SUCCESS;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。