码迷,mamicode.com
首页 > 其他好文 > 详细

readline库的使用

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

标签:io   ar   使用   sp   for   on   bs   ad   line   

接口十分简单,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库的使用

标签:io   ar   使用   sp   for   on   bs   ad   line   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!