一、简介

由于项目中写了个测试的控制台程序,是每次读取一行,即通过getline()来实现的,所以每次必须输入全路径名称,才能实现运行。

大家都觉得麻烦,就写了个tab键自动选择补全的。

目前基本可实现功能,但是没有回退的功能和跳出循环,有需用的就自己改吧。

此程序基于windows的控制台实现的。

二、实现过程

1. 代码自我感觉还算清楚。里面有部分注释。

#include<stdio.h>
#include <string>
#include <conio.h>
#include <map>
using namespace std;
#include<windows.h>
#include<stdlib.h>
#include<wincon.h> // tab键 获取相关值,并打印到屏幕上。
void tab_find_char(std::map<int,char*> store_compare_,char* store_input,int i_store_input_count,int& tab_count);
// 从字符中找到相关字符,并返回相关字符。
char* get_attach(const char* stacks_,const char* needle_); void console_start();
void console_end(); void write(const char* message,int length);
void write(const char* message);
void read(char* buffer, size_t size);
#include <iostream>
#include "conio.h"
// 记录屏幕光标位置
static COORD curser_position; void main()
{
console_start();
// 自定义的可搜索库字符集
static std::map<int,char*> store_compare;
store_compare[0] = "thread_parameter_";
store_compare[1] = "parameter_";
store_compare[2] = "build";
store_compare[3] = "compile"; int count=0;
char ch;
while (true)
{
int tab_count = 0;
char store_input[256];
int i_store_input_count = 0;
// 记录光标位置
HANDLE hOut;
CONSOLE_SCREEN_BUFFER_INFO bInfo;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &bInfo );
curser_position.X = bInfo.dwCursorPosition.X;
curser_position.Y = bInfo.dwCursorPosition.Y; do
{
ch =(char)_getch();
if (ch !='\n')
{
if (ch == 'q')
{
break;
}
else if (ch == 9)
{
tab_find_char(store_compare,store_input,i_store_input_count,tab_count);
tab_count++;
}
// 接受数字和全部字母和部分特殊符号。
else if( (ch >= 48 && ch <= 57) || (ch >= 65 && ch <= 122) )
{
char* temp_char = new char[1];
temp_char[0] = ch;
write(temp_char,1);
store_input[i_store_input_count] = ch;
i_store_input_count++;
}
}
}
while (ch!='\r');
write("\n");
}
console_end();
} void tab_find_char(std::map<int,char*> store_compare_,char* store_input,int i_store_input_count,int& tab_count)
{
if (i_store_input_count < 0 || tab_count < 0)
{
return;
} int map_size = store_compare_.size();
tab_count = tab_count % map_size; i_store_input_count += 1;
store_input[i_store_input_count-1] = '\0';
char* pri_temp_input = new char [i_store_input_count];
strncpy(pri_temp_input,store_input,i_store_input_count);
int i_catch_times = 0; HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); for (int i = 0;i < map_size;i++)
{
char* pri_get_supply = get_attach(store_compare_[i],pri_temp_input);
if (NULL != pri_get_supply)
{
if (tab_count > i_catch_times)
{
i_catch_times++;
continue;
} //clear
SetConsoleCursorPosition(hOut,curser_position);
string out_put_string = " ";
write(out_put_string.c_str()); SetConsoleCursorPosition(hOut,curser_position);
write(store_compare_[i]);
break;
}
}
delete pri_temp_input;
pri_temp_input = NULL;
i_store_input_count -= 1;
return;
} char* get_attach(const char* stacks_,const char* needle_)
{
char* pri_string_stack = (char*)stacks_;
char* pri_string_needle = (char*)needle_;
char* pri_string = strstr(pri_string_stack,pri_string_needle);
return pri_string;
} /*---------------------------------------------------------*/ void console_start()
{
AllocConsole(); DWORD dwConsoleMode;
GetConsoleMode(NULL, &dwConsoleMode);
dwConsoleMode ^= ENABLE_LINE_INPUT;
dwConsoleMode ^= ENABLE_ECHO_INPUT;
//wConsoleMode ^= ENABLE_AUTO_POSITION; SetConsoleMode(NULL, dwConsoleMode);
const char* message = "welcome! test started. atuhor by zhang pengju\n";
write(message);
} void console_end()
{
FreeConsole();
} void write(const char* message,int length)
{
if (length <= 0)
{ return;
}
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), message, length, NULL, NULL);
} void write(const char* message)
{
int length = strlen(message);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), message, length, NULL, NULL);
} void read(char* buffer, size_t size)
{
DWORD nNumberOfCharsToRead;
BOOL result = ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, size, &nNumberOfCharsToRead, NULL);
buffer[nNumberOfCharsToRead - 2] = 0;
}

2.在vs2012下 测试结果是可以的。就不给贴图了。

运行程序,测试输入,t,然后切换tab键就可以看见有自动补齐的功能。

输入_,有不同的单词供你选择,然后回车,可换其他测试。



    不足之处,请多指教。





全部源码都在这里了。也就不给资源链接啥的。

C++ tab键实现自动补全输入功能的更多相关文章

  1. CentOS中按tab键不能自动补全问题解决办法

    CentOS中按tab键不能自动补全问题解决办法 一:检查一下系统有没有安装bash-completion包. 二:yum查找一下 三:yum安装bash-completion包 前言 在CentOS ...

  2. CentOS 8 按tab键不能自动补全问题解决方案

    CentOS中按tab键不能自动补全问题解决办法 检查一下系统有没有安装bash-completion包 [root@Sonarqube ~]# rpm -lq bash-completion yum ...

  3. Unbuntu 终端中使用Tab键不能自动补全

    解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc 2.找到文件中的下列代码 #enable bash com ...

  4. SecureCRT ,可是进入模拟器后TAB键还是无法补全

    SecureCRT是做网络,路由,交换机等设备的人都知道的工具 ,可是进入模拟器后TAB键还是无法补全,就很懊恼了. 设置步骤: 1)打开SecureCRT软件,选项—全局选项—常规—默认的会话设置— ...

  5. 利用redis完成自动补全搜索功能(一)

    最近要做一个搜索自动补全的功能(目前只要求做最前匹配),自动补全就是自动提示,类似于搜索引擎,再上面输入一个字符,下面会提示多个关键词供参考,比如你输入 nb 2字符, 会自动提示nba,nba录像, ...

  6. UITextFiled自动补全输入,选中补全内容。NSRange和UITextRange的相互转换。-b

    有个需求就是 需要用户输入几位以后账号,可以根据本地存储的登录成功的账号,进行自动补全,并且补全内容为选中状态,不影响用户的新输入. 研究了一下,下面是完整的实现的方法. 补充个下载地址http:// ...

  7. TextBox 设置数据源的自动补全输入字符串功能

    这个东西首先说明是不是自己原创,但是比较简单.所以讲起分享如下.主要是用到TextBox的自动补全属性,这个东西虽然自己以前经常用TextBox,但是补全从没接触过. 关键代码是在窗体载入时加载如下代 ...

  8. linux下让irb实现代码自动补全的功能

    我不知道其他系统上irb是否有此功能,但是在ubuntu上ruby2.1.2自带的irb默认是没有代码自动补全功能的,这多少让人觉得有所不便.其实加上也很简单,就是在irb里加载一个模块:requir ...

  9. 利用redis完成自动补全搜索功能(三)

    前面已经完成了分词和自动提示功能,最后把搜索结合在一起,来个完成的案例.当然最好还是用搜索分词解决,这个只是一个临时解决方案. 其实加上搜索很简单,要做的就是3件事 1. 分词的时候,把有用词的id存 ...

随机推荐

  1. nyoj 214 单调递增子序列(二) 【另类dp】

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列.并求出其长度 ...

  2. cgroups

    CGROUPS官方解析,用户空间怎样监控 http://blog.chinaunix.net/uid-16763274-id-2103750.html cgroups概念 fr=aladdin&quo ...

  3. BZOJ 1231 状压DP

    思路: f[i][j] i表示集合的组成 j表示选最后一个数 f[i][j]表示能选的方案数 f[i|(1<< k)][k]+=f[i][j]; k不属于i j属于i且符合题意 最后Σf[ ...

  4. Ajax往后台传参数,无参数,一个参数,多个参数,一个对象等

    原文:http://www.cnblogs.com/chenwolong/p/Get.html //无参数请求-简单示例 $(document).ready(function () { $.ajax( ...

  5. Gym 100952 C. Palindrome Again !!

    http://codeforces.com/gym/100952/problem/C C. Palindrome Again !! time limit per test 1 second memor ...

  6. OA项目笔记

    一.创建项目构架 1.创建一个Maven的web工程 1.1修改编译器版本 <properties> <project.build.sourceEncoding>UTF-8&l ...

  7. Python基础教程之第3章 使用字符串

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...

  8. 【剑指Offer面试题】九度OJ1384:二维数组中的查找

    下决心AC全部剑指offer面试题. 九度OJ面试题地址:http://ac.jobdu.com/hhtproblems.php 书籍:何海涛--<剑指Offer:名企面试官精讲典型编程题> ...

  9. Linux-清除rootpassword

    1.Linux进入单用户模式,通常选择内核按e 最后加入single,按b进入单用户. 2.清除/etc/shadow中root这一行第一个:符号和第二个:符号之间内容. 3.正常启动,passwd ...

  10. 用 cctld工具创建带有国家代码的IP地址表

    用 cctld工具创建带有国家代码的IP地址表 cctld tools is creating IP addresses table with Country Code 项目地址 https://gi ...