创建具有挑战性的终端菜单

发布于 2024-10-03 11:41:41 字数 1040 浏览 3 评论 0原文

我不会做的是创建一个终端菜单,它接受各种类型的参数并将其放置在数组参数中。下面是代码: 这是我遇到的一些问题,但找不到好的解决方案。

如果我只是输入“列表”,我会得到“不是有效的命令,我必须输入”列表“(列表和空格)。 新的菜单选项应该是这样的:新的“我的名字是你好”。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-10-10 11:41:41

您正在对“”进行定界。

fgets 读取 ENTER。

因此,当您键入“listENTER”并在空格处标记时,您会得到一个标记,即“listENTER”。后来你与“list”进行比较,当然不匹配。

尝试

strtok(line, " \n"); /* maybe include tabs too? */

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

strtok(line, " \n"); /* maybe include tabs too? */

PS. Why are you calling menu recursively? You already have a while in the function ...

歌枕肩 2024-10-10 11:41:41

你的问题是 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.

月野兔 2024-10-10 11:41:41

我不确定这是否会导致您的问题,但这些行

        /*new(param[1]);

        /*list();

Start a comment 永远不会终止。

如果您想要一行注释,您可以使用:(

       // comment

至少在 C++ 中以及从 C99 开始)

但是以 /* 开头的注释必须以 */ 结束,并且不能嵌套:

/* comment */

/* also multi line
   allowed */

由于您在注释中开始注释,您的编译器应该发出警告,实际上这根本不应该编译。

I'm not sure if this causes your problem but these lines

        /*new(param[1]);

        /*list();

Start a comment that is never terminated.

If you want one line comments you can use:

       // comment

(atleast in C++ and from C99 on)

But comments starting with /*must be ended with a */and not nested:

/* comment */

/* also multi line
   allowed */

Since you start a comment in a comment your compiler should have emmited a warning, actually this shouldn't compile at all.

握住我的手 2024-10-10 11:41:41

您需要输入“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 of strtok 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 in line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文