创建具有挑战性的终端菜单
我不会做的是创建一个终端菜单,它接受各种类型的参数并将其放置在数组参数中。下面是代码: 这是我遇到的一些问题,但找不到好的解决方案。
如果我只是输入“列表”,我会得到“不是有效的命令,我必须输入”列表“(列表和空格)。 新的菜单选项应该是这样的:新的“我的名字是你好”。 param[0] = new 和 param[1] = 我的名字是 hello ,(所以我可以创建一条带有空格的消息)。
我怎样才能做到这一点?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int menu()
{
printf(">");
char line[LINE_MAX];
int i = 0;
char *param[4];
while(fgets(line, LINE_MAX, stdin) != NULL) {
param[i++] = strtok(line, " \n");
if(param[0] != NULL) {
char *argument;
while((argument = strtok(NULL, "\n")) != NULL) {
param[i++] = argument;
}
}
if(strcmp(param[0], "new") == 0) {
//new(param[1]);
menu();
} else if(strcmp(param[0], "list") == 0) {
//list();
menu();
} else {
printf("Not a valid command.\n\n");
menu();
}
}
return 0;
}
What I wont to do is to create a terminal menu that takes various types of arguments and place it in a array param. Under is the code: Here is some trouble that I have and cant find a good solution for.
if i just type 'list' I will get Not a valid command, I have to type “list “ (list and space).
Menu choice new should be like this: new “My name is hello”. param[0] = new and param[1] = My name is hello , (sow I can create a message with spaces).
How can I accomplish this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int menu()
{
printf(">");
char line[LINE_MAX];
int i = 0;
char *param[4];
while(fgets(line, LINE_MAX, stdin) != NULL) {
param[i++] = strtok(line, " \n");
if(param[0] != NULL) {
char *argument;
while((argument = strtok(NULL, "\n")) != NULL) {
param[i++] = argument;
}
}
if(strcmp(param[0], "new") == 0) {
//new(param[1]);
menu();
} else if(strcmp(param[0], "list") == 0) {
//list();
menu();
} else {
printf("Not a valid command.\n\n");
menu();
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在对“”进行定界。
fgets
读取 ENTER。因此,当您键入“listENTER”并在空格处标记时,您会得到一个标记,即“listENTER”。后来你与“list”进行比较,当然不匹配。
尝试
PS。为什么要递归调用
menu
?您的函数中已经有一个while
...You're delimiting on " ".
fgets
reads the ENTER.So, when you type "listENTER" and tokenise at spaces you get one token, namely "listENTER". Later you compare with "list" and, of course, it doesn't match.
Try
PS. Why are you calling
menu
recursively? You already have awhile
in the function ...你的问题是 param[i++] = strtok(line, " ");只会在空格上分割,而不是在 \n (换行符)上分割。尝试将其添加到您的分隔符数组中。
哦,恭喜你获得了一些看起来不错、干净且格式良好的代码。一个令人愉快的变化。
Your problem is param[i++] = strtok(line, " "); will only split on space, not on \n (newline). Try adding this to your array of delimeters.
Oh, and congratulations for some decent looking code that's clean and well formatted. A pleasant change.
我不确定这是否会导致您的问题,但这些行
Start a comment 永远不会终止。
如果您想要一行注释,您可以使用:(
至少在 C++ 中以及从 C99 开始)
但是以
/*
开头的注释必须以*/
结束,并且不能嵌套:由于您在注释中开始注释,您的编译器应该发出警告,实际上这根本不应该编译。
I'm not sure if this causes your problem but these lines
Start a comment that is never terminated.
If you want one line comments you can use:
(atleast in C++ and from C99 on)
But comments starting with
/*
must be ended with a*/
and not nested:Since you start a comment in a comment your compiler should have emmited a warning, actually this shouldn't compile at all.
您需要输入“list”的原因是您的第一个
strtok
标记直到空格字符,因此在这种情况下您需要输入一个。尝试同时允许'\n'
和空格作为分隔符,即用" \n"
替换strtok
的第二个参数。对于引号,您需要重新组合参数,从以引号开头的参数到以 1 结尾的参数,将参数之间的字符替换为空格。或者取消
strtok
并通过手动迭代line
中的字符进行解析。The reason you need to type "list " is that your first
strtok
tokenizes until a space character, so you need to enter one in this case. Try allowing both'\n'
and space as separators, i.e. replace the second parameter ofstrtok
with" \n"
.As for quotes, you need to re-combine parameters starting from the one beginning with a quote to the one ending with one by replacing the characters in between them with spaces. Or do away with
strtok
and parse by manually iterating through the characters inline
.