How to get output of system() command

Hi,
im trying to execute a CMD command trough system()
What i now want is to get that output as a string.

I want something like this:

string output = system(command);
if (output == "Bad command or filename."){
cout <<"Syntax error"<<endl;

Does anyone have any idea how i could ever do anything like that?
closed account (48bpfSEw)
Hi, years ago I had the same problem. Here is one of the solutions (answer of Trolli)

https://www.c-plusplus.net/forum/topic,16485.html


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <windows.h>
 
int cmd(char *cmd, char *output, DWORD maxbuffer)
{
    HANDLE readHandle;
    HANDLE writeHandle;
    HANDLE stdHandle;
    DWORD bytesRead;
    DWORD retCode;
    SECURITY_ATTRIBUTES sa;
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
   
    ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
    ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
    ZeroMemory(&si,sizeof(STARTUPINFO));
   
    sa.bInheritHandle=true;
    sa.lpSecurityDescriptor=NULL;
    sa.nLength=sizeof(SECURITY_ATTRIBUTES);
    si.cb=sizeof(STARTUPINFO);
    si.dwFlags=STARTF_USESHOWWINDOW;
    si.wShowWindow=SW_HIDE;
 
    if (!CreatePipe(&readHandle,&writeHandle,&sa,NULL))
    {
        OutputDebugString("cmd: CreatePipe failed!\n");
        return 0;
    }
 
    stdHandle=GetStdHandle(STD_OUTPUT_HANDLE);
 
    if (!SetStdHandle(STD_OUTPUT_HANDLE,writeHandle))
    {
        OutputDebugString("cmd: SetStdHandle(writeHandle) failed!\n");
        return 0;
    }
 
    if (!CreateProcess(NULL,cmd,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi))
    {
        OutputDebugString("cmd: CreateProcess failed!\n");
        return 0;
    }
 
    GetExitCodeProcess(pi.hProcess,&retCode);
    while (retCode==STILL_ACTIVE)
    {
        GetExitCodeProcess(pi.hProcess,&retCode);
    }
 
    if (!ReadFile(readHandle,output,maxbuffer,&bytesRead,NULL))
    {
        OutputDebugString("cmd: ReadFile failed!\n");
        return 0;
    }
    output[bytesRead]='\0';
 
    if (!SetStdHandle(STD_OUTPUT_HANDLE,stdHandle))
    {
        OutputDebugString("cmd: SetStdHandle(stdHandle) failed!\n");
        return 0;
    }
 
    if (!CloseHandle(readHandle))
    {
        OutputDebugString("cmd: CloseHandle(readHandle) failed!\n");
    }
    if (!CloseHandle(writeHandle))
    {
        OutputDebugString("cmd: CloseHandle(writeHandle) failed!\n");
    }
 
    return 1;
}
 
int WINAPI WinMain(HINSTANCE hInstanceHandle,
                   HINSTANCE hPreviousInstanceHandle,
                   LPSTR lpszCommandLine,
                   INT nShowState) {
 
    char PingOutput[2048];
    cmd("ping", PingOutput, 2048);
    MessageBox(NULL, PingOutput, "Ping-Output", MB_OK);
    return EXIT_SUCCESS;
}





Hope it works...
Yes it works!, i thought it didnt for a sec, but just entered the wrong stuff XD
Thanks!
Last edited on
closed account (48bpfSEw)
Ok, I checked it. It works!

The output of "ping" (help text) is shown in the MessageBox!

Did you opend a DOS Command and run the exe within?
im sorry, i entered the wrong codes and thought it failed XD
Topic archived. No new replies allowed.