如何通过C/ c++从Linux命令中获得输出?并且适用于android
How to get output from Linux command via C/C++? and suitable for android?
我尝试运行一个Linux命令,并使用C/c++代码读取它的输出。我搜索了exec,但是这个不处理输入/输出。
我想要实现的是通过使用这个命令iwconfig
来获取有关无线局域网的信息,从C/c++代码调用它。
我还需要一个合适的代码使用它作为lib android使用NDK。
我看到在android开源中他们叫这个函数
你觉得这段代码怎么样?
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;
}
这是链接
您可以尝试运行该命令并将结果输出到文件中,然后读取它
system("iwconfig > temp.txt");
FILE *fp=fopen("temp.txt","w");
我看到在android开源中他们叫这个函数
你觉得这段代码怎么样?
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;
}
这是链接
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何使用ndk-build.cmd构建Android.so文件
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- Android NDK传感器向事件队列报告奇怪的间隔
- 为什么我的代码在输出中增加了93天
- Android Studio 3.2:cmake message()输出显示在哪里
- Android系统模块不会在构建上产生输出
- wcstombs()在Android上的输出无效
- Android OpenCV imread 标志致命异常,imread 在没有标志的情况下工作正常,像素输出与 MATLAB 不匹配
- 如何通过JNI/NDK获得Android应用中使用的c++库的日志行(printf, cout等)的控制台输出
- Tensorflow会话.Run在Android设备上运行时给出空输出张量,但在Ubuntu上运行时工作良好
- 如何通过C/ c++从Linux命令中获得输出?并且适用于android
- 是否可以在Android Studio中使用.so作为输出构建一个本地模块
- 在Android OpenCV中,输出字节[]需要cv::imencode
- QtCreator:如何在Android上部署时查看程序输出