| VC++编写交互程序时将打印结果输出到文件或cmd窗口显示 |
| 来源:
发布时间:2008-05-27 发布人:
浏览:
人次
字体:
[大
中
小]
|
|
当我们在进行C++程序开发时,需要打印一些结果出来,但是输出时由于屏幕显示有限,看不到全部结果,下面这个函数可以将cout语句的输出输出到一个指定的文件中去,方便我们的查看 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
void coutToFile(char *fileName) { int fd; fd = open(fileName, O_WRONLY | O_CREAT, 0644); chmod(fileName, S_IRWXU | S_IRWXG | S_IRWXO); close(1); dup(fd); }
int main( int argc, char** argv ) { coutToFile("./tmp.log"); ........ return 0; } 我是在LINUX下使用的 使用后,终端屏幕上不再显示输出信息,而是输出到了指定的文件中去了 在使用VC++编写交互程序时,由于都是交互界面,所以运行中cout的信息是看不到的,使用下面的方法可以在你的交互程序运行的同时弹出一个cmd窗口,所有cout的信息全部输出到该窗口中
BOOL CTestApp::InitInstance() { AfxEnableControlContainer(); ........... // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE;
#ifdef _DEBUG if (!AllocConsole()) AfxMessageBox("Failed to create the console!", MB_ICONEXCLAMATION); #endif
// The main window has been initialized, so show and update it. pMainFrame->ShowWindow(SW_SHOWMAXIMIZED); pMainFrame->UpdateWindow();
// CG: This line inserted by 'Tip of the Day' component. ShowTipAtStartup();
return TRUE; }
最后,在退出时别忘了删除该对象 int CTestApp::ExitInstance() { #ifdef _DEBUG if (!FreeConsole()) AfxMessageBox("Could not free the console!"); #endif
return CWinApp::ExitInstance(); } 由于使用了宏定义_DEBUG,所以只在Debug版本下才有,release版本下没有
| |
| |
|
|
|
|
| §最新评论:(评论内容只代表网友观点,与本站立场无关!) | |
|
|
|
|
| 注意:请勿在本站发布政治话题、色情及违反法律的内容。 |
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。 |