Question

Ici, je veux exécuter une commande et je dois donner ma contribution à cette commande après avoir exécuté la première.

je veux exécuter obex_test Commande pour le mode Bluetooth qu'après avoir dû donner des entrées comme «S» pour le serveur de démarrage, alors comment puis-je donner cette chose.voici mon code, j'écris ce truc et j'ai obtenu un résultat.j'ai eu une erreur de données d'entrée après l'exécution obex_test commande.

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

}

sortir :

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

De la sortie après cette ligne OBEX Interactive test client/server. je dois donner le caractère d'entrée 's' mais je ne peux pas le faire directement, sa boucle est infinie et printf >Unknown command.

Était-ce utile?

La solution

oh, si vous voulez donner une entrée à la commande popen par votre fichier c, essayez de cette façon

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

ici si vous voulez donner l'option, écrivez 's'

fp est le pointeur de fichier de popen()

ça fonctionne bien

dans ton 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);

}

Modifier:surmonter pour une boucle infine

à chaque fois, donnez deux nouveaux caractères de ligne après avoir donné une option

comme

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

Autres conseils

Si vous souhaitez transmettre des caractères (options stdin), vous pouvez utiliser fputc ou mieux encore, 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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top