简体   繁体   English

如何通过C / C ++从Linux命令获取输出? 并适合Android?

[英]How to get output from Linux command via C/C++? and suitable for android?

I try to run a Linux command and read the output from it by using C/C++ code. 我尝试运行Linux命令并使用C / C ++代码从中读取输出。 I search for exec but this don't deal with input/output. 我搜索exec但这不涉及输入/输出。

What I am trying to achieve is to get information about wireless LAN by using this command iwconfig , invoking it from C/C++ code. 我想要实现的是通过使用此命令iwconfig获取有关无线LAN的信息,从C / C ++代码调用它。

also i need a suitable code to use it as lib for android using NDK. 我还需要一个合适的代码,使用NDK作为Android的lib。

i see in android open source they called this function 我在android开源中看到他们调用了这个函数

what do you think about this code ? 你怎么看待这段代码?

int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
                 char *reply, size_t *reply_len,
                 void (*msg_cb)(char *msg, size_t len))
                {
        DWORD written;
        DWORD readlen = *reply_len;

if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
    return -1;

if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
    return -1;
*reply_len = readlen;

return 0;

} }

this is the link 这是链接

You could try running the command and outputting the results to a file, then reading it 您可以尝试运行该命令并将结果输出到文件,然后读取它

system("iwconfig > temp.txt");
FILE *fp=fopen("temp.txt","w");

i see in android open source they called this function 我在android开源中看到他们调用了这个函数

what do you think about this code ? 你怎么看待这段代码?

int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
                     char *reply, size_t *reply_len,
                     void (*msg_cb)(char *msg, size_t len))
{
    DWORD written;
    DWORD readlen = *reply_len;

    if (!WriteFile(ctrl->pipe, cmd, cmd_len, &written, NULL))
        return -1;

    if (!ReadFile(ctrl->pipe, reply, *reply_len, &readlen, NULL))
        return -1;
    *reply_len = readlen;

    return 0;
}

this is the link 这是链接

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

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