首页 > 其他 > 详细

readline库的使用

时间:2014-11-20 00:03:14      阅读:315      评论:0      收藏:0      [点我收藏+]

接口十分简单,readline和addhistory:


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>


int main()
{
    char* input, shell_prompt[100];


    // Configure readline to auto-complete paths when the tab key is hit.
    rl_bind_key(‘\t‘, rl_complete);


    for(;;) {
        // Create prompt string from user name and current working directory.
        snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));


        // Display prompt and read input (n.b. input must be freed after use)...
        input = readline(shell_prompt);


        // Check for EOF.
        if (!input)
            break;


        // Add input to history.
        add_history(input);


        // Do stuff...


        // Free input.
        free(input);
    }
}


g++ TestReadLine.cc -lreadline就可以了。

readline库的使用

原文:http://blog.csdn.net/jollyjumper/article/details/41292771

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!