获取cmd输出结果

执行cmd命令,获取输出结果。

方式一:

void GetConsoleResult()
{
    FILE* pPipe = _popen("cmd /c tasklist |findstr /i \"cmd.exe\"", "r");
    std::string strOut;
    if (pPipe)
    {
        /* Read pipe until end of file, or an error occurs. */
        char   psBuffer[10] = {0};
        while (fgets(psBuffer, 10, pPipe))
        {
            strOut.append(psBuffer);
            printf(psBuffer);
        }


        /* Close pipe and print return value of pPipe. */
        if (feof(pPipe))
        {
            printf("\nProcess returned %d\n", _pclose(pPipe));
        }
        else
        {
            printf("Error: Failed to read the pipe to the end.\n");
        }

    }

}

 

你可能感兴趣的:(获取cmd输出结果)