獲取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");
        }

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章