2012-12-21 60 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 
相關問題