【问题标题】:Can Anybody Explain me popen() with write mode任何人都可以用写模式解释我 popen()
【发布时间】:2012-02-20 04:55:56
【问题描述】:

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

我想在蓝牙模式下执行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 &gt;Unknown command。

【问题讨论】:

    标签: c bluetooth popen obex


    【解决方案1】:

    哦,如果你想通过你的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);
    
    }
    

    编辑:克服 for infine 循环

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

    喜欢

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

    【讨论】:

    • 我把这个放在哪里了?这两行?
    • 是的,我已经验证了此类基于菜单的示例。您可以在 popen() 命令之后和 pclose() 之前将这一行放在你的代码中
    • @Mr.32 在这方面遇到问题。当我执行这个程序并按 ctrl+c 时,它会进入无限循环
    • @user1089679 取决于您的 obex_test 二进制代码,您还需要在从您的主函数执行之前退出该菜单系统..
    • @user1089679 了解整个概念并解决问题,您将轻松获得解决方案
    【解决方案2】:

    如果您想传递字符(stdin 选项),您可以使用 fputc 或更好,fputs

    #include <stdio.h>
    #include <fcntl.h>
    
    int main(int argc, char *argv[])
    {
        FILE *fp = popen("/path/to/yourBinary cmdLineArgs", "w");
    
        if (fp == NULL)
        {
            printf("Failed to open the pipe\n");
        }
        else
        {
            // Option1: pass in character, by character
            fputc('n', fp);
            fputc('\n', fp);
    
            // Option2: provide 's' to the binary with the new line
            fputs("s\n", fp);
    
            // fputs allows multiple characters
            fputs("4096M\n", fp);
    
            pclose(fp);
        }
    
       return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2020-09-21
      相关资源
      最近更新 更多