如何在控制台应用程序中创建命令历史记录? [英] How to make command history in console application?

查看:195
本文介绍了如何在控制台应用程序中创建命令历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要进行交互式控制台应用程序,这允许在循环中输入命令。例如,用户类型搜索和程序找到一些数据并将其打印在屏幕上。然后程序等待下一个命令(可以是搜索,退出或其他)。为了方便用户,我希望我的程序支持命令历史(如在终端,当按下键盘上的上下箭头)。但我不知道该怎么做,因为我不知道如何打印文本,可以通过scanf,std :: getline,std :: cin等进一步阅读。
所以代码 std :: cin<< hello; 未编译(对于'operator<<'in'std :: cin<<hello')没有匹配。函数 fprintf(stdin,hello); 不打印任何内容,scanf无法读取此打印的消息。显然, std :: getline(std :: cin,str); scanf(%s,s); gets(s)等不能读取由 printf 输出的文本 std :: out 。所以问题是:我如何在控制台上打印文本,也将在 stdin std :: cin ) ?或者也许有更优雅的方式来组织命令历史?

I want to make interactive console application, which allows to enter command in loop. For example, user types "search" and program finds some data and prints it on the screen. Then the program waits for the next command (which can be search, quit or other). For user's convenience I want my program supports command history (like in terminal, when pressing up and down arrow on the keyboard). But I can't realize how to do that because I don't know how to print text which can be read further by scanf, std::getline, std::cin and so on. So code std::cin << "hello"; isn't compiled (no match for ‘operator<<’ in ‘std::cin << "hello"’). Function fprintf(stdin, "hello"); prints nothing and scanf can't read this printed message. It is evident that std::getline(std::cin, str); and scanf("%s", s); and gets(s) and so on can't read text which has been outputted by printf or std::out. So the question is: how can I print text on console which also will be in stdin (std::cin)? Or maybe there is more elegant way to organize command history?

我也想过模拟按钮按下来打印我需要的文本,但我希望有更好的方式来创建命令历史

P.S. I also thought about simulating of button pressing to print the text I need, but I hope there is better way to make command history

P.P.S。我使用Linux和C ++

P.P.S. I use Linux and C++

推荐答案

使用 readline 历史这是为了这个目的。

Use the readline and history libraries, which are made exactly for that purpose.

这篇关于如何在控制台应用程序中创建命令历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆