【问题标题】:How to read output and give input to a program from c program?如何从 c 程序读取输出并为程序提供输入?
【发布时间】:2009-05-29 03:41:27
【问题描述】:

这是参考以下问题:

Execute program from within a C program

如何使用 Tiny C 编译器在 Windows 上执行相同的操作? 我需要从 c 程序执行 .exe 并通过使用文件或字符串作为源从同一个 C 程序中为其提供输入,并将其输出读入字符串或文件。我一直在使用 system() 函数。欢迎任何建议或示例..在此先感谢

【问题讨论】:

    标签: c windows input system


    【解决方案1】:

    如果您没有 popen() 等,或者您想避免复杂性,最简单的方法是简单地编写一个数据文件,例如。使用 fwrite() 的 infile 使用 system() 执行外部程序,然后读取 outfile。

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

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

    这样您就可以轻松地使用输入/输出文件的内容对其进行测试。 您通常会在 tmp 目录中执行此操作,然后在完成后将其删除。

    唯一需要注意的是.exe的路径

    【讨论】:

      【解决方案2】:

      谷歌搜索“windows popen”,你会发现这个链接:

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

      C 运行时库也有 _popen(),但我建议不要这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多