简体   繁体   English

在C中,我编写了一个使用system()调用exe的程序。 exe要求用户输入。 如何让我的程序提供输入?

[英]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.

相关问题 如何从 C 程序调用 notepad.exe? - How can I call notepad.exe from a C program? 如何制作扫描用户输入(文本)并将其保存在动态字符串上的C程序 - How can i make C program that scans user's input(text) and save it on a dynamic string C 如何通过系统调用程序并暂停该程序? - C how can I call an program over system and pause that program? 我正在尝试验证用户输入。 但是,如果我输入了无效字符,程序将进入无限循环 - I am trying to validate the user input. But if i entered an invalid character the program goes to an infinite loop 如何修改 c 程序,以便在用户输入非浮点输入时,向用户显示“无效输入”? - How can I modify the c program such that if the user enters a non-float input,“invalid input” is shown to the user? 一个简单的C程序可以反转输入的每一行。 我完成了 但我不断得到$ - A simple C program to reverse every line of input. I have finished it. But I keep getting �$ 在 C 中,如何向先前使用 system() 调用的程序提供输入? - In C, how do I give an input to a program previously called with system()? 我在 c 语言的 for 循环中提供输入后,我的程序就停止工作了 - My program just stops working after I provide input in a for loop of c language 我编写了一个程序来获取用户输入并将其写入文本文件但在获得两个输入后它卡住了 - I wrote a program to get user input and write it in a text file But it stuck after getting two inputs 我怎样才能使程序请求用户输入以关闭它或从头开始启动它 - how can i make the program request user input to either close it or start it from the beginning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM