我可以在哪里学习如何让c++程序与操作系统(Linux)交互

时间:2021-07-17 20:44:59

I am a C++ beginner.

我是c++初学者。

I'd like to create small programs that interact with the operating system (using Kubuntu Linux). So far, I have not been able to locate any tutorial or handbook to get C++ to interface with the OS.

我想创建与操作系统交互的小程序(使用Kubuntu Linux)。到目前为止,我还没有找到任何教程或手册来获得c++与操作系统的接口。

In PHP, I can use the command exec() or the backtick operator to launch commands typically executed in a console. How can I do similar things in C++? How can I get my C++ program to execute any other command? How can I get the output of such commands?

在PHP中,我可以使用命令exec()或backtick操作符来启动控制台中通常执行的命令。我如何在c++中做类似的事情?如何让c++程序执行其他命令?如何获得这些命令的输出?

Thanks.

谢谢。

3 个解决方案

#1


6  

You can use the system() command in stdlib to execute system commands:

您可以使用stdlib中的system()命令执行系统命令:

#include <stdlib.h>
int main() {
    system("ls -l");
}

system() returns an int as its return value, but the value of the int is system-dependent. If you try and use a command that doesn't exist, you'll get the standard "no such command" output back, and usually a non-zero return value. (For example, running system("ls -l"); on my Windows XP machine here returns a value of 1.

system()返回一个int作为它的返回值,但是int的值是系统依赖的。如果您尝试使用一个不存在的命令,您将得到标准的“no such command”输出,并且通常返回一个非零的返回值。(例如,运行系统(“ls -l”);在我的Windows XP机器上,这里返回1的值。

#2


7  

You can use system() to execute arbitrary commands but, if you want to easily control the input and output with the program, you should look into popen().

您可以使用system()执行任意命令,但是,如果您想轻松地控制程序的输入和输出,您应该查看popen()。

For even more control, you can look into doing exactly what a shell might do, creating some extra file descriptors, forking to start another process, setting up file descriptors 0, 1 and 2 (input, output and error) in that process to connect them to your original process file descriptors then exec'ing the program you want to control. This isn't for the faint of heart :-)

更多的控制,你可以考虑做壳可能做什么,创建一些额外的文件描述符,分支启动另一个流程,设置文件描述符0、1和2(输入、输出和错误)在这个过程中,将它们连接到您的原始过程文件描述符然后exec操作程序你想控制。这不是为胆小的人准备的

#3


2  

You can use system() as instructed previously or you can use libraries which provide access to standard POSIX API. unistd.h and The GNU C Library include many functions for OS interaction. The possibilities with these libraries are endless as you can implement the functionalities yourself. A simple example: scan a directory for text files with scandir() and print the contents of the files.

您可以使用以前所指示的系统(),也可以使用提供标准POSIX API访问的库。unistd。h和GNU C库包含许多用于操作系统交互的函数。使用这些库的可能性是无限的,因为您可以自己实现这些功能。一个简单的示例:使用scandir()扫描文本文件的目录并打印文件的内容。

#1


6  

You can use the system() command in stdlib to execute system commands:

您可以使用stdlib中的system()命令执行系统命令:

#include <stdlib.h>
int main() {
    system("ls -l");
}

system() returns an int as its return value, but the value of the int is system-dependent. If you try and use a command that doesn't exist, you'll get the standard "no such command" output back, and usually a non-zero return value. (For example, running system("ls -l"); on my Windows XP machine here returns a value of 1.

system()返回一个int作为它的返回值,但是int的值是系统依赖的。如果您尝试使用一个不存在的命令,您将得到标准的“no such command”输出,并且通常返回一个非零的返回值。(例如,运行系统(“ls -l”);在我的Windows XP机器上,这里返回1的值。

#2


7  

You can use system() to execute arbitrary commands but, if you want to easily control the input and output with the program, you should look into popen().

您可以使用system()执行任意命令,但是,如果您想轻松地控制程序的输入和输出,您应该查看popen()。

For even more control, you can look into doing exactly what a shell might do, creating some extra file descriptors, forking to start another process, setting up file descriptors 0, 1 and 2 (input, output and error) in that process to connect them to your original process file descriptors then exec'ing the program you want to control. This isn't for the faint of heart :-)

更多的控制,你可以考虑做壳可能做什么,创建一些额外的文件描述符,分支启动另一个流程,设置文件描述符0、1和2(输入、输出和错误)在这个过程中,将它们连接到您的原始过程文件描述符然后exec操作程序你想控制。这不是为胆小的人准备的

#3


2  

You can use system() as instructed previously or you can use libraries which provide access to standard POSIX API. unistd.h and The GNU C Library include many functions for OS interaction. The possibilities with these libraries are endless as you can implement the functionalities yourself. A simple example: scan a directory for text files with scandir() and print the contents of the files.

您可以使用以前所指示的系统(),也可以使用提供标准POSIX API访问的库。unistd。h和GNU C库包含许多用于操作系统交互的函数。使用这些库的可能性是无限的,因为您可以自己实现这些功能。一个简单的示例:使用scandir()扫描文本文件的目录并打印文件的内容。