简体   繁体   English

从C程序调用命令行

[英]Call command line from C program

I am writing an command line application in C and from within the program, I would like to call other command-line applications. 我在C中编写命令行应用程序,从程序中,我想调用其他命令行应用程序。 For example (and only as a clear example!), say the I would like to write a C app that calls the java compiler. 例如(并且仅作为一个明确的例子!),说我想写一个调用java编译器的C app。 For one, how would I do this? 首先,我该怎么做? Is there a C function that directly calls the command line of something? 是否有一个直接调用命令行的C函数? For two, how would I take the normal text output that the java compiler displays and display it through the c program? 对于二,我如何获取java编译器显示的正常文本输出并通过c程序显示它?

I'm sorry if this sounds a bit crazy, but I am very new to C and I have never done anything like this before. 我很抱歉,如果这听起来有点疯狂,但我对C很新,我之前从未做过这样的事情。 I would love to start writing a few of these utility style apps. 我很想开始编写一些这样的实用程序风格的应用程序。 If it matters any, I am currently running Windows, but would prefer a platform-independent method if possible, as I do use Linux from time to time. 如果重要的是,我目前正在运行Windows,但如果可能的话,我更喜欢与平台无关的方法,因为我不时使用Linux。

You might look into system . 您可以查看system I think you can use it in Windows as well as UNIX/Linux systems. 我认为你可以在Windows以及UNIX / Linux系统中使用它。

Use the right tool for the job. 使用正确的工具完成工作。 C is not meant for programming this sort of 'utility'. C不适用于编程这种“实用程序”。 Use either a shell scripting language, or something like Python or Perl. 使用shell脚本语言,或Python或Perl之类的东西。

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

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