简体   繁体   English

如何从C程序读取输出并为程序提供输入?

[英]How to read output and give input to a program from c program?

This is with reference to the below question: 这是参考以下问题:

Execute program from within a C program 从C程序中执行程序

How do I do the same on Windows with Tiny C Compiler? 在Windows上使用Tiny C编译器该如何做? I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. 我需要执行一个.exe程序,并使用文件或字符串作为源从同一个C程序中为其提供输入,并将其输出读取为字符串或文件。我一直在使用system()函数。 Any suggesstions or examples are welcome..Thanks in advance 欢迎任何建议或示例。

The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. 如果没有popen()等,或者想避免复杂性,最简单的方法就是简单地编写数据文件,例如。 infile with fwrite() execute the external program with system() and then read outfile. 使用fwrite()的infile使用system()执行外部程序,然后读取outfile。

system("prog.exe <infile >outfile") 

Your prog.exe only has to read stdin and write stdout. 您的prog.exe只需要读取stdin和写入stdout。

This way you can easily test it with the contents of in/out file. 这样,您可以使用输入/输出文件的内容轻松对其进行测试。 You would normally do this in your tmp directory and then delete them when you are finished. 通常,您可以在tmp目录中执行此操作,然后在完成后将其删除。

The only thing to be careful of is the path to the .exe 唯一要注意的是.exe的路径

Google for "windows popen" and you find this link: Google为“ windows popen”提供了此链接:

lists.trolltech.com/qt-interest/1999-09/thread00282-0.html lists.trolltech.com/qt-interest/1999-09/thread00282-0.html

The C runtime library also has _popen(), but I would recommend against it. C运行时库也具有_popen(),但我建议不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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