如何重定向 stderr ?

可以使用该方法写一个轻量级日志类

#include <cstdio>
int main()
{
	FILE *stream = freopen("freopen.txt", "w", stderr);
	if (stream) {
		fprintf(stdout, "successfully reassigned\n");
		fflush(stdout);
		fprintf(stream, "This will go to the file freopen.txt\n");
		fprintf(stderr, "Also you can do it like this!\n");
		fclose(stream);
	}else{
		fprintf(stdout, "error on freopen\n");
	}

	system("type freopen.txt");
	getchar();
    return 0;
}
posted @ 2019-03-20 17:30  學海無涯  阅读(641)  评论(0编辑  收藏  举报