ncurses 菜单 - 不会显示我的用户输入的字符串
当我创建一个文字字符串并将其添加到菜单中时,一切正常。 但是如果我从用户输入一个字符串,那么菜单是“空白”。 我不知道这是一个curses/menu问题,还是一个C问题,因为我对这两个问题都是初学者。
#include <curses.h>
#include <menu.h>
#include <malloc.h>
int main()
{
MENU *my_menu;
ITEM **my_items;
char c;
// works
char my_string[20] = "this is the string";
// user-inputted string, comment these 2 lines out to make this program work
printf("enter something: ");
fgets(my_string, 19, stdin);
initscr();
noecho();
crmode();
my_items = (ITEM **)calloc(2, sizeof(ITEM *));
my_items[0] = new_item(my_string, my_string);
my_items[1] = (ITEM *)NULL;
my_menu = new_menu(my_items);
post_menu(my_menu);
refresh();
while ((c = getch()) != 'q') { }
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
return 0;
}
When I create a literal string and add it to the menu, everything works fine. But if I input a string from the user, then the menu is "blank". I don't know if this is a curses/menu problem, or a C problem, as I am a beginner at both.
#include <curses.h>
#include <menu.h>
#include <malloc.h>
int main()
{
MENU *my_menu;
ITEM **my_items;
char c;
// works
char my_string[20] = "this is the string";
// user-inputted string, comment these 2 lines out to make this program work
printf("enter something: ");
fgets(my_string, 19, stdin);
initscr();
noecho();
crmode();
my_items = (ITEM **)calloc(2, sizeof(ITEM *));
my_items[0] = new_item(my_string, my_string);
my_items[1] = (ITEM *)NULL;
my_menu = new_menu(my_items);
post_menu(my_menu);
refresh();
while ((c = getch()) != 'q') { }
free_item(my_items[0]);
free_item(my_items[1]);
free_menu(my_menu);
endwin();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是输入字符串末尾的“\n”。 删除它将使这项工作正常进行。
The problem was the '\n' at the end of the inputted string. Removing that will make this work.