Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从其他命令行界面程序获取输出?_C++_Linux_Io_Terminal_Iwconfig - Fatal编程技术网

C++ 如何从其他命令行界面程序获取输出?

C++ 如何从其他命令行界面程序获取输出?,c++,linux,io,terminal,iwconfig,C++,Linux,Io,Terminal,Iwconfig,好吧,我做了一些研究,但我找不到任何有用的东西。我正在尝试编写一个程序,该程序将从iwconfig(在linux机器上)接收输入。然后,它将对输入进行排序,进行一些计算并输出到数据库。对输入和输出进行排序不是问题(或者我真的希望不是这样),但我正在努力从另一个命令行程序读取输入。我现在的Hello World基础课程是: #include <iostream> #include <cstdlib> using namespace std;

好吧,我做了一些研究,但我找不到任何有用的东西。我正在尝试编写一个程序,该程序将从iwconfig(在linux机器上)接收输入。然后,它将对输入进行排序,进行一些计算并输出到数据库。对输入和输出进行排序不是问题(或者我真的希望不是这样),但我正在努力从另一个命令行程序读取输入。我现在的Hello World基础课程是:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main() {
        int numbr = 0;
        cout << "Hello world!" << endl;
        cin >> numbr;
        cout << "number is " << numbr;
        cout << system("iwconfig");
        return 0; 
    }
#包括
#包括
使用名称空间std;
int main(){
int numbr=0;
不能麻木;
库特
“有人能解释一下我如何运行iwconfig这样的程序并捕获它的输出吗?”

检查文档。它当然不提供返回值,您希望使用
cout
语句输出

您可能希望在主进程和
iwconfig
程序之间建立管道,以控制子进程使用的输入和输出流

要复制上述答案,请执行以下操作:

int main() {
    int fd_p2c[2], fd_c2p[2], bytes_read;
    pid_t childpid;
    char readbuffer[80];
    string program_name = "iwconfig";
    string receive_output = "";

    if (pipe(fd_p2c) != 0 || pipe(fd_c2p) != 0) {
        cerr << "Failed to pipe\n";
        exit(1);
    }
    childpid = fork();

    if (childpid < 0) {
        cout << "Fork failed" << endl;
        exit(-1);
    }
    else if (childpid == 0) {
        if (dup2(fd_p2c[0], 0) != 0 ||
            close(fd_p2c[0]) != 0 ||
            close(fd_p2c[1]) != 0) {
            cerr << "Child: failed to set up standard input\n";
            exit(1);
        }
        if (dup2(fd_c2p[1], 1) != 1 ||
            close(fd_c2p[1]) != 0 ||
            close(fd_c2p[0]) != 0) {
            cerr << "Child: failed to set up standard output\n";
            exit(1);
        }

        execl(program_name.c_str(), program_name.c_str(), (char *) 0);
        cerr << "Failed to execute " << program_name << endl;
        exit(1);
    }
    else {
        close(fd_p2c[0]);
        close(fd_c2p[1]);

        cout << "Writing to child: <<" << gulp_command << ">>" << endl;
        int nbytes = gulp_command.length();
        if (write(fd_p2c[1], gulp_command.c_str(), nbytes) != nbytes) {
            cerr << "Parent: short write to child\n";
            exit(1);
        }
        close(fd_p2c[1]);

        while (1) {
            bytes_read = read(fd_c2p[0], readbuffer, sizeof(readbuffer)-1);

            if (bytes_read <= 0) break;

            readbuffer[bytes_read] = '\0';
            receive_output += readbuffer;
        }

        close(fd_c2p[0]);
        cout << "From child: <<" << receive_output << ">>" << endl;
    }
    return 0;
}
intmain(){
int fd_p2c[2],fd_c2p[2],字节读取;
pid_t childpid;
字符读取缓冲区[80];
字符串程序\u name=“iwconfig”;
字符串receive_output=“”;
如果(管道(fd|p2c)!=0 |管道(fd|c2p)!=0){

cerr欢迎来到StackOverflow!你可能想精简你的帖子,因为它有很多无关的信息。你的问题似乎是问你如何运行
iwconfig
,并捕获它的输出,所以上面一段中的大部分信息都是不必要的。不过,有代码和对不起作用的内容的描述是很好的,希望有人能帮你。