工程1:新建一个对话框工程,并放一个按钮,按钮函数调用f(),f是把控制台程序输出内容输出到e:\\txalarm.txt中.
void f()
{
HANDLE hOutFile;
hOutFile=CreateFile("e:\\txalarm.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);SECURITY_ATTRIBUTES saPipe; /* security for anonymous pipe */ 
saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
saPipe.lpSecurityDescriptor = NULL;
saPipe.bInheritHandle = TRUE;HANDLE hReadPipe, hWritePipe,hWritePipe2;
BOOL bSuccess = CreatePipe(&hReadPipe, /* read handle */
&hWritePipe, /* write handle, used as stdout by child */
&saPipe, /* security descriptor */
0); /* pipe buffer size */bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hReadPipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
NULL, /* new handle - don't want one, change original handle */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
FALSE, /* make it *not* inheritable */
DUPLICATE_SAME_ACCESS);bSuccess = DuplicateHandle(GetCurrentProcess(), /* source process */
hWritePipe, /* handle to duplicate */
GetCurrentProcess(), /* destination process */
&hWritePipe2, /* new handle, used as stderr by child */
0, /* new access flags - ignored since DUPLICATE_SAME_ACCESS */
TRUE, /* it's inheritable */
DUPLICATE_SAME_ACCESS);PROCESS_INFORMATION pi; 
STARTUPINFO si; 
memset(&si,0,sizeof(si)); 
si.hStdInput=hReadPipe;
si.hStdOutput=hWritePipe;
si.hStdError=hWritePipe2;
si.dwFlags=STARTF_USESTDHANDLES;
si.cb=sizeof(si); ZeroMemory( &pi, sizeof(pi) );BOOL bRet=CreateProcess("E:\\console.exe",NULL,NULL,FALSE,TRUE,NULL,NULL,NULL,&si,&pi); 
WaitForSingleObject(pi.hThread,INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);CloseHandle(hWritePipe);
CloseHandle(hWritePipe2);char chReadBuffer[256];
DWORD cchReadBuffer=sizeof(chReadBuffer);for (;;)
{
bSuccess = ReadFile(hReadPipe, /* read handle */
chReadBuffer, /* buffer for incoming data */
sizeof(chReadBuffer), /* number of bytes to read */
&cchReadBuffer, /* number of bytes actually read */
NULL); /* no overlapped reading */
if (!bSuccess && (GetLastError() == ERROR_BROKEN_PIPE))
break; /* child has died */if (bSuccess && cchReadBuffer)
{
/* write the data from the child to the file */
bSuccess = WriteFile(hOutFile, /* write handle */
chReadBuffer, /* buffer to write */
cchReadBuffer, /* number of bytes to write */
&cchReadBuffer, /* number of bytes actually written */
NULL); /* no overlapped writing */
}
}
CloseHandle(hOutFile);
CloseHandle(hReadPipe);
}//f//按钮函数 
void CpipeDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
f();//调用控制台重定向输出.
}//----------------------
工程2:另外新建一个控制台程序 console.exe
// console.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[])
{
for(int i=0;i<100;i++)
puts("1111111111111111111111111111111111111"); return 0;
}
然后把console.exe放到e:, 然后运行工程1,可以在e:\\txalarm.txt下看到控制台输出的内容一致.
但现在碰到2个问题:
1.如果把 console.cpp中的for改为:for(int i=0;i<500;i++),那么运行工程1就会出现死机的状况.
2.这个可以实现打开控制台程序,但只有程序执行完毕后才能输入到文件,我想让他时时的向文件只输入请高手帮忙怎么样才能实现呀!!急呀!!老板在头上发火呢!

解决方案 »

  1.   

    问题1:你可能输出太快,导致超出了缓冲区的限制。建议检测puts的返回值。问题2:如果你想实时写文件,应该在需要写入的地方调用FlushFileBuffers(hOutFile),这将强迫写入硬盘文件。
      

  2.   

    谢谢这位大哥.
    关于你说的第一点,我没明白.
    我直接运行console 工程,是没啥问题的.控制台输出正常.哪里缓冲区异常了?
      

  3.   

    直接输出concole是没问题,系统帮你做好了,问题是你已经重定向到你自己的管道中了,而管道都是有缓冲区限制的,意思是你从管道中取出数据的速度太慢,会导致缓冲区溢出,从而导致写管道的操作失败。具体原因你最好还是查看返回值。
      

  4.   

    对对,是这个现象.但不知道这么看呢?大哥可以具体说说吗?看put返回值,啥意思?
      

  5.   

    楼上说了,写完就直接FLUSH,强制写到硬盘中去。。