Question

Here i want to Execute one Command and i have to give input to this command after executing first one.

i want to execute obex_test command for bluetooth mode than after i have to give input like 's' for starting server so how can i give this thing. here is my code i write this thing and got output. i got error of input data after executing obex_test command.

Code:

#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);

}

output :

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

From Output after this line OBEX Interactive test client/server. i have to give input character 's' but i cant able to do this direclty its goes infinite loop and printf >Unknown command.

Was it helpful?

Solution

oh if you want to give input to popen command by your c file then try this way

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

here if you want to give s option then write 's'

fp is file pointer of popen()

it works fine

in your code :

#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);

}

Edit: to overcome for infine loop

every time give two new line character after giving any option

like

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

OTHER TIPS

If you want to pass characters (stdin options) you can use fputc or better yet, 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top