谁能用写模式解释我的 popen()

标签 c bluetooth popen obex

这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。

我想执行 obex_test蓝牙模式命令 而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。 这是我的代码,我写了这个东西并得到了输出。执行 obex_test 后出现输入数据错误命令。

代码:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

void main() {

    char *input = "obex_test -b";   
    FILE *fp = NULL;
    char path[512];


    fp = popen(input, "w");

    if (fp == NULL) 
    {
        printf("\nFailed command\n");
        return;
    }
    else
    {
        printf("\nSuccesss command\n");
    }
    printf("starting while : %d", fp);

    while (fgets(path, sizeof(path) - 1, fp) != NULL) {

        printf("\nOutput    :::  %s \n", path);
    }

    printf("\nEnd\n");
    /* close */
    pclose(fp);

}

输出:

Successs command
starting while : 69640
End
Using Bluetooth RFCOMM transport
OBEX Interactive test client/server.
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command
> Unknown command

从这一行之后的输出 OBEX Interactive test client/server.我必须给输入字符“s”,但我无法直接执行此操作,它进入无限循环和 printf >Unknown command .

最佳答案

哦,如果你想通过你的c文件给popen命令输入,那么试试这种方式

fputc ( 's', fp );
fputc ( '\n', fp);

这里如果你想给 s 选项然后写 's'

fp 是popen() 的文件指针

一切正常

在你的代码中:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

void main() {

    char *input = "obex_test -b";   
    FILE *fp = NULL;
    char path[512];


    fp = popen(input, "w");

    if (fp == NULL) 
    {
        printf("\nFailed command\n");
        return;
    }
    else
    {
        printf("\nSuccesss command\n");
    }

//giving s option to menu
fputc ( 's', fp );
fputc ( '\n', fp);



    printf("starting while : %d", fp);

    while (fgets(path, sizeof(path) - 1, fp) != NULL) {

        printf("\nOutput    :::  %s \n", path);
    }

    printf("\nEnd\n");
    /* close */
    pclose(fp);

}

编辑:克服无限循环

每次给出任何选项后给出两个换行符

喜欢

//giving s option to menu
fputc ( 's', fp );
fputc ( '\n', fp);
fputc ( '\n', fp);

关于谁能用写模式解释我的 popen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9029284/

相关文章:

c - 使用未分配的空间 calloc

python - 以其他用户身份运行 Popen 时出现的问题 (macOS)

c++ - 执行 popen(),将 FILE* 指针放入 fstream,那么 pclose() 呢?

python - 如何在 python 中获取命令输出而不是子进程?

c - Signal::expose-event 对 GdkWindow 和 GdkX11Window 无效

c - gcc 4.4 与 Visual C++ 2008

c - 使用后结构体属性发生变化

ios - iPhone Objective C 蓝牙免提音频增加 8000 采样率

android - Bluetooth LE Android 平板电脑无法与 Lego Boost Move Hub 连接

java - Android:回调 java.lang.NullPointerException 中的 sendBroadcast 未处理的异常