[英]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");
我在C中编写了一个程序,它调用了另一个我写的
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");
一旦
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.
然后,我希望程序显示哪些输入导致输出字符串2。
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. 换句话说,程序1请求输入并显示字符串A,B或C.程序2使用不同的输入多次运行程序1,并显示哪些输入导致字符串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
). 您可能不想使用
system
,而是使用popen
(或者,在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). 这使您可以打开子程序并为您提供一个
FILE *
,您可以通过该FILE *
读取子项的标准输出,或者写入子项的标准输入(取决于您是指定“r”还是“w”作为打开模式)。 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. 如果您不能执行后者,因为您似乎在Windows上执行此操作,您可以/可以使用
CreateProcess
创建具有重定向输入和输出的子项。 It's much more versatile, but also a lot more work. 这是更灵活,但也有很多更多的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.