这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。
我想执行 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/