简体   繁体   中英

In C, I wrote a program to call an exe using system(). The exe asks for user input. How can I make my program provide the input?

I've seen posts that are like this, but I couldn't find the exact answer to my question. I wrote a program in C that calls another exe I wrote by saying system("./program_name"); . The program that is called asks for user input, and based on what is input, displays one of three strings to the screen.

Once system("./program_name"); runs the program, is there some way I can also make my program provide the input? I want this program to be able to run the other one multiple times, by providing different inputs each time. Then, I would like the program to display which inputs caused string 2 to be output.

In other words, program 1 asks for input and displays String A, B, or C. Program 2 runs program 1 several times with different inputs, and displays which inputs resulted in String B.

I'm stuck on being able to provide the input from my program.

Thanks in advance for your help!! :)

Instead of using system , you probably want to use popen (or, in Microsoft-land, _popen ). This lets you open the child program and gives you a FILE * through which you can read the child's standard output, or write to the child' standard input (depending on whether you specify "r" or "w" as the open mode). Although non-standard, some reasonably recent versions allow both reading and writing (which it sounds like you'd really prefer).

If you can't do the latter, since you seem to be doing this on Windows, you can/could use CreateProcess to create the child with both input and output redirected. It's much more versatile, but also a lot more work.

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