2012-12-21 110 views
4

我在C执行CMD线++如何保存执行输出CMD线在文件中的C++

#include <windows.h> 
#include <stdio.h> 
#include <tchar.h> 

void _tmain(int argc, TCHAR *argv[]) 
{ 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 

    ZeroMemory(&si, sizeof(si)); 
    si.cb = sizeof(si); 
    ZeroMemory(&pi, sizeof(pi)); 

    if(argc != 2) 
    { 
     printf("Usage: %s [cmdline]\n", argv[0]); 
     return; 
    } 

    // Start the child process. 
    if(!CreateProcess(NULL, // No module name (use command line) 
     argv[1],  // Command line 
     NULL,   // Process handle not inheritable 
     NULL,   // Thread handle not inheritable 
     FALSE,   // Set handle inheritance to FALSE 
     0,    // No creation flags 
     NULL,   // Use parent's environment block 
     NULL,   // Use parent's starting directory 
     &si,   // Pointer to STARTUPINFO structure 
     &pi)   // Pointer to PROCESS_INFORMATION structure 
    ) 
    { 
     printf("CreateProcess failed (%d).\n", GetLastError()); 
     return; 
    } 

    // Wait until child process exits. 
    WaitForSingleObject(pi.hProcess, INFINITE); 

    // Close process and thread handles. 
    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 
} 

我想救执行输出file.but如何代码?

回答

3

在CreateProcess的你传递一个STARTUPINFO结构。您可以在si.dwFlags中设置STARTF_USESTDHANDLES,然后用有效的文件描述符填充hStdInput,hStdOutput和hStdError字段,特别是hStdOutput应该是先前打开的文件(由成功的CreateFile返回)的句柄,然后它将接收标准输出已启动的进程。

编辑:

这是一种平均的答案,因为它需要更多的工作,以使该事情的工作:你需要创建一个合适的SECURITY_ATTRIBUTES这个文件并且在Set handle inheritance设置为true的CreateProcess 。所以这也是纯粹主义的噩梦。

#include <windows.h> 
#include <stdio.h> 
#include <tchar.h> 

void _tmain(int argc, TCHAR *argv[]) 
{ 
    STARTUPINFO si; 
    PROCESS_INFORMATION pi; 

    ZeroMemory(&si, sizeof(si)); 
    si.cb = sizeof(si); 
    si.dwFlags |=STARTF_USESTDHANDLES ; 
    si.hStdInput=GetStdHandle(STD_INPUT_HANDLE); 
    si.hStdError=GetStdHandle(STD_ERROR_HANDLE); 
    SECURITY_ATTRIBUTES sa; 
    ZeroMemory(&sa, sizeof(sa)); 
    sa.nLength=sizeof(sa); 
    sa.bInheritHandle=TRUE; 
    si.hStdOutput=CreateFile ("log.txt", GENERIC_READ|GENERIC_WRITE, 0, &sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 
    ZeroMemory(&pi, sizeof(pi)); 

    if(argc != 2) 
    { 
     printf("Usage: %s [cmdline]\n", argv[0]); 
     return; 
    } 
    // Start the child process. 
    if(!CreateProcess(NULL, // No module name (use command line) 
     argv[1],  // Command line 
     NULL,   // Process handle not inheritable 
     NULL,   // Thread handle not inheritable 
     TRUE,   // Set handle inheritance to TRUE 
     0,    // No creation flags 
     NULL,   // Use parent's environment block 
     NULL,   // Use parent's starting directory 
     &si,   // Pointer to STARTUPINFO structure 
     &pi)   // Pointer to PROCESS_INFORMATION structure 
    ) 
    { 
     printf("CreateProcess failed (%d).\n", GetLastError()); 
     return; 
    } 
    // Wait until child process exits. 
    WaitForSingleObject(pi.hProcess, INFINITE); 

    // Close process and thread handles. 
    CloseHandle(pi.hProcess); 
    CloseHandle(pi.hThread); 
    CloseHandle (si.hStdOutput); 
} 
3

无论是输出重定向标准输出到一个文件

freopen("file.txt", "w", stdout); 

或者,管到一个文件与Windows

cmd> prg.exe > file.txt 
相关问题