简体   繁体   中英

Program Get Stuck After Opening a Program Using System in c++

I want to open a program (that has terminal like CMD) with a C++ code, but when I try System() and that program opens, my C++ code get stuck until I close the program and the rest of the code will execute. I want to open a program that is a terminal and after opening, I want to send some commandS to program to execute it. how can I:

1 - Continue to the rest of the C++ code after calling System() function

2 - Send a command to program using C++ code.

3 - receive command result

Here is the simple code that I use:

#include <iostream>

int main()
{
    system("path of the program");
    
    while(true)
    {
        // some commands that I want to send to terminal
        // receive result 
        //....
    }
}

The system function in Windows C runtime library is "blocking". That means that the system command does not return until the program you are running with it exits. If you must open a program from within your C++ code, you'll need to start a new process for it so that your code can continue running (see this answer for some information on how to do that).

To send and receive information between your two programs, you'll need to implement some form of inter-process communication (ipc). See this reference for how to get started on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM