gpt4 book ai didi

c - 如何从 bash 向 C 程序提供输入?

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:52 24 4
gpt4 key购买 nike

<分区>

我已经阅读了大量有关 bash 脚本和程序测试的资料,但我仍然无法使这段代码正常工作。

基本上它是一个简单的程序,要求用户选择north east southwest。我启动程序,然后它立即要求输入。我只是无法让 bash 脚本给它任何输入。我尝试使用 echoexpect

感谢任何帮助。

这是用来获取玩家输入的函数:

int process_input(Map *game)
{
printf("\n> ");

char ch = getchar();
char a = getchar(); //eat enter

int damage = rand() % 4;

switch(ch) {

case -1:
printf("you suck\n");
return 0;
break;
case 'n':
game->proto.move(game, NORTH);
break;
case 's':
game->_(move)(game, SOUTH);
break;
case 'e':
game->_(move)(game, EAST);
break;
case 'w':
game->_(move)(game, WEST);
break;
case 'a':
game->_(attack)(game, damage);
break;
case 'l':
printf("You can go:\n");
if(game->location->north) printf("NORTH\n");
if(game->location->south) printf("SOUTH\n");
if(game->location->east) printf("EAST\n");
if(game->location->west) printf("WEST\n");
break;
default:
printf("Whats next?", ch);
}
return 1;
}

这是对 bash 脚本的尝试:

   #!/bin/bash
/Desktop/c
./ex17 echo 'w'

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com