这是我想要做的: 用户需要在提示符处输入命令,程序将评估命令以了解要执行的操作。 如果未找到该命令,则shell将返回错误“Command not found!”。 如果用户输入命令EXIT,则程序应退出。 为了简单起见并避免用户按下输入而没有输入任何内容的问题,我们将假设用户将始终输入内容。 不会使用空字符串。我应该在这里循环这是我想做的事情:
示例:
shell.exe
壳牌和GT;测试
找不到命令!
壳牌和GT; TEST2
找不到命令!
壳牌和GT;出口
>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char command[20];
while (command != "exit") {
//check with an if statement for valid commands
printf("Enter a command:\n");
gets(command);
}
if (command == "exit") {
//program should exit
}
return 0;
}
答案 0 :(得分:-1)
使用popen()
或system()
。 Popen为i / o提供了一个处理程序,而系统给出了一个狗屎,但你仍然可以在命令行上重定向到一个管道(&#34;&#;;&#34;&#34;&gt;&#34;)从那里读取和写入i / o。
答案 1 :(得分:-1)
如果你使用while循环,你应该给命令数组一个字符串值,这样就可以在循环条件下进行检查。
char exit[4] = "exit";
gets(command);
if (strcmp(command,exit) == 0)
{
//Exit programm
}
while (strcmp(command,exit)) != 0)
{
printf("Command not found!"\n Enter a command \n);
gets(command);
if (strcmp(command,exit) == 0)
{
//Exit programm
}
}