【发布时间】:2013-02-27 15:10:41
【问题描述】:
我正在编写一个 shell,我正在使用 getline() 和键盘的标准输入来接收命令。我在标记输入时遇到了麻烦。我尝试在 strtok() 函数中使用 \n 作为分隔符,但它似乎不起作用。
例如,我包含了一个 if 语句来检查用户是否键入了“exit”,在这种情况下它将终止程序。它没有终止。
这是我正在使用的代码:
void main() {
int ShInUse = 1;
char *UserCommand; // This holds the input
int combytes = 100;
UserCommand = (char *) malloc (combytes);
char *tok;
while (ShInUse == 1) {
printf("GASh: "); // print prompt
getline(&UserCommand, &combytes, stdin);
tok = strtok(UserCommand, "\n");
printf("%s\n", tok);
if(tok == "exit") {
ShInUse = 0;
printf("Exiting.\n");
exit(0);
}
}
【问题讨论】:
-
您是否尝试过使用调试器查看正在读取的内容?如果没有,您应该这样做。