避免使用控制序列(例如C或C ++标准输入流中的^ [[C)) [英] Avoid control sequences(like ^[[C) from C or C++ standard inputstream

查看:55
本文介绍了避免使用控制序列(例如C或C ++标准输入流中的^ [[C))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s;
    cout << "Enter a string : " << endl;
    cin >> s;
    cout << "The Entered String is : " << s << endl;
    cout << "The Length of Entered String is : " << s.length() << endl;
    return 0;
}

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

如果您想用C中的代码,也请C发生同样的事情!

Same thing happens with C also if you want the code in C please ask for it!

当我按下箭头键^ [[C时出现,而不是光标移动(其他箭头键,退出键,home,end也会发生类似的情况!)!

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

这是字符串第二次包含以下字符的情况:

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

因此,'\ x1b','[','C'是从键盘发送到外壳的字符序列,用于表示向右箭头键(光标向前).

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

我想要的是这些字符不会显示在外壳中,但是光标会移动(按所按下的键向前,向后,到原点,结束等).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

输入后进行处理的意义不大,因为主要目的是让光标移动!

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

如何用C或C ++实现呢?

How can i achieve this in C or C++?

唯一的目的是在使用程序时为用户提供精确的终端体验.

The one and only aim is to give the user a exact terminal like experience while using the program.

推荐答案

在不同的终端中,光标移动,功能键和其他特殊键的处理方式不同.但是在大多数情况下,某些 转义序列 生成.

Cursor movement, function keys and other special keys are handled differently in different terminals. But in most cases some escape sequence is generated.

大多数语言中的程序提示是从 stdin 读取的简单内容,它将按原样读取转义序列.所以←[D ←[C 与左/右光标移动相对应.

Program prompt in most languages is a simple read from stdin, it will read escape sequences as-is. So e.g. ←[D, ←[C correspond to left/right cursor movement.

如果您要处理这些转义序列,如 bash 提示符一样,那么您需要自己执行此操作或使用第三方库.

If you want to handle these escape sequences like bash prompt does, then you need to do that yourself or use a 3rd-party library.

一种流行的是GNU readline ,但也有其他选择,例如 libedit .

A popular one is GNU readline, but there are alternatives, e.g. libedit.

这是一个带有libedit的演示程序(需要先安装libedit: sudo apt install libedit-dev ):

Here's a demo program with libedit (need to install libedit first: sudo apt install libedit-dev):

#include <editline/readline.h>
#include <stdio.h>

int main() {
    char *line;
    while (line = readline("Enter input: ")) {
        printf("Entered: %s\n", line);
    }
    return 0;
}

编译并运行:

$ g++ test.cpp -o test -ledit
$ ./test

现在,您应该可以使用左右键在输入行中移动.

Now you should be able to move around the input line with left/right keys.

这篇关于避免使用控制序列(例如C或C ++标准输入流中的^ [[C))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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