【发布时间】:2014-08-20 11:11:05
【问题描述】:
我试图用 C 语言制作一个类似于 DOS 的 shell 解释器(显然是为了好玩)
当我输入 clear 时,如下面的代码所示,它应该可以清除屏幕。但事实并非如此。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char command[128];
int loop = 0;
void main(){
clrscr();
printf("Starting shell\n");
clrscr();
while ( loop == 0){
printf("command:");
scanf("%s", &command);
if(command=='clear'){
printf("Clearing screen");
clrscr();
}
/** Other Code **/
【问题讨论】:
-
scanf("%s", command); if(!strcmp(command, "clear")){ printf("Clearing screen"); clrscr(); } -
这里有很多基本的 C 错误。阅读有关这些函数的文档并做一些 C 教程。使用
scanf("%s", command)和if(!strcmp(command, "clear"))。 C 中的字符串使用双引号,而不是单引号。 -
不要使用
scanf("%s", command),而是使用fgets(command, sizeof commmand, stdin)。