如何阅读输出并给予输入从C程序的程序? [英] How to read output and give input to a program from c program?

查看:152
本文介绍了如何阅读输出并给予输入从C程序的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参照以下的问题:


从C程序内执行程序

我该怎么做Windows上的相同与微小的C编译器?
我需要执行一个.exe文件来回C程序,并通过使用文件或者字符串作为源给来自同一个C程序中输入到它,并从中读取输出到一个字符串或fi​​le.I一直在使用系统()frunction。任何suggesstions或例子都是提前welcome..Thanks

This is with reference to the below question:

Execute program from within a C program

How do I do the same on Windows with Tiny C Compiler? 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. Any suggesstions or examples are welcome..Thanks in advance

推荐答案

如果你没有popen方法最简单的方法()等等,或者你想避免的复杂性,是simplly写入数据文件,例如。与fwrite的INFILE()与系统执行外部程序(),然后读取OUTFILE。

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. infile with fwrite() execute the external program with system() and then read outfile.

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

您prog.exe只需要标准输入读取和写入标准输出。

Your prog.exe only has to read stdin and write stdout.

这样你就可以轻松地与输入/输出文件的内容进行测试。
你通常会做你的tmp目录中,然后删除它们,当你完成。

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.

要小心的唯一的事情就是路径的.exe

The only thing to be careful of is the path to the .exe

这篇关于如何阅读输出并给予输入从C程序的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆