可视化如何保存执行的输出CMD行在文件中的c++



我有这段代码在c++中执行CMD行

#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 );
}

我想在文件中保存执行的输出。但如何?

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

编辑:

这是一种平均的答案,因为它需要更多的工作来使它工作:你需要用正确的SECURITY_ATTRIBUTES创建那个文件,并且必须在CreateProcess中将Set handle inheritance设置为TRUE。这样做也是纯粹主义者的噩梦。

#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);
}

将stdout重定向到文件
freopen("file.txt", "w", stdout);

或者,用windows

将输出管道到文件中
cmd> prg.exe > file.txt

最新更新