freopen ("CON", "w", stdout);
freopen ("CON", "r", stdin);
freopen ("CON", "w", stderr);
如果仍然无法正常工作,请尝试。
// Start : edit
SDL_Init (...)
FILE * ctt = fopen("CON", "w"); // c
// or
ofstream ctt("CON"); // c++
// End : edit
freopen ("CON", "w", stdout);
freopen ("CON", "r", stdin);
freopen ("CON", "w", stderr);
/* ... */
// Start : edit
fclose (ctt); // c
// or
ctt.close(); // c++
// End : edit
了解有关CON here。
从我看来,它似乎可以用NULL替换“CON”。
(对于它的价值。)
freopen(NULL,"w",stdout);
freopen(NULL,"w",stdout);
freopen(NULL,"w",stderr);
的freopen函数方法的其它变种。
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
有关CONOUT $和CONIN的更多信息$ here。
如果您在使用GNU/Linux BSD,Solaris,Mac Os等软件时遇到问题,请尝试以下操作。
freopen ("/dev/tty", "w", stdout);
freopen ("/dev/tty", "r", stdin);
freopen ("/dev/tty", "w", stderr);
应用该方法应该看起来像下面的代码。
/* ... */
int main {
SDL_Init(SDL_INIT_VIDEO);
/* start : redirecting the streams to the console */
FILE * ctt = fopen("CON", "w"); // Edit
freopen ("CON", "w", stdout);
freopen ("CON", "r", stdin);
freopen ("CON", "w", stderr);
/* end : redirecting the streams to the console */
/*start : your code */
putenv("SDL_VIDEO_CENTERED=center");
SDL_WM_SetCaption("Railroad Builder",NULL);
SDL_WM_SetIcon(IMG_Load(strcat_const(parentFolder(exePath),
"/icon.png")),NULL);
SDL_SetVideoMode(MAIN_WINDOW_WIDTH,MAIN_WINDOW_HEIGHT,32,
SDL_OPENGL);
int running = 1;
while(running){
printf("myVar = %d",myVar);
}
/* end : your code */
/* ... */
fclose (ctt); // Edit
/* ... */
}
你可以在SDL faq上找到这个例子。
2 - 更改程序的入口点
您也可以取消定义SDL的主让你的主要将是第一个被调用。
为此,请在主功能之前添加下一条指令。
/*...*/
#ifdef main
#undef main // Prevent SDL from overriding the program's entry point.
#endif
/***/
int main(int argc, char **argv){
/*...*/
}
或
/*...*/
#ifdef __MINGW32__ // It can be __MINGW64__. Chose according to your architecture.
#undef main // Prevent SDL from overriding the program's entry point.
#endif
//or
#if defined(__MINGW32__) || defined(__MINGW64__)
#undef main // Prevent SDL from overriding the program's entry point.
#endif
/*...*/
int main(int argc, char **argv){
/*...*/
}
关于MINGW32 and MINGW64
3 - 重建SDL
随着从SDL网站,你不能阻止标准输出/标准错误下载预编译的二进制SDLmain.dll从被重定向到文本文件。 你可以做的是使用NO_STDIO_REDIRECT编译器标志自己编译SDLmain,或者根本不使用SDLmain。
请注意,不使用SDLmain会破坏可移植性,因此不推荐使用。
也就是说,有时将stdout.txt和stderr.txt写入可执行文件所在的目录会更好。
你可以用一个小诡计做到这一点:
#include "SDL/SDL.h"
#ifdef WIN32
#include "sdl_winmain.h"
#else
#include "SDL/SDL_main.h"
#endif
凡sdl_winmain.h是在自己的项目目录,并从SDL源码包的src/main/SDL_win32_main.c的改写副本。通过这种方式,您仍然可以在其他平台上使用可移植性,但无法在Windows中获得stdout/stderr文件。
这是从SDL faq。欲了解更多信息,请致电wiki。
3.5在源
在源代码中(〜/ SDL2-2.0.4 /包括/ SDL_main.h)我发现下面
#ifndef SDL_MAIN_HANDLED
#if defined(__WIN32__)
/* On Windows SDL provides WinMain(), which parses the command line and passes
the arguments to your main function.
If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
*/
的一个在线副本source code。
如果您没有成功定义NO_STDIO_REDIRECT,请尝试SDL_MAIN_HANDLED。
这是无处可在SDL-1.2.15中(〜/ SDL-1.2.15 /包括/ SDL/SDL_main.h)不过,我确实发现是
#define main SDL_main
,似乎是按照从上面的#undef主要方法。
4 - 注意
这可能是有趣的,发送错误信息到stderr,而不是标准输出。为此,请尝试使用perror或fprintf。
像这样:
/*...*/
perror ("Error"); // the string cannot be formatted
fprintf (stderr,"Error %s", str_err_msg); // the string can be formatted
/*...*/
你能告诉我们你正在使用的代码吗? – byxor
@BrandonIbbotson在这里。 –
[SDL控制台输出可能在调试时起作用,但不能在使用exe时运行](http://stackoverflow.com/questions/14593783/sdl-console-output-works-when-debuging-but-not-when -run -with-the-exe) –