readline の使い方 in C++2008年09月23日 13時51分53秒

C++ 言語での readline の使い方。

//  c++ rl.cpp -lreadline -lcurses

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string>
#include <iostream>

int main()
{
    using namespace std;

    char *ptr;
    string line;
    using_history();
    read_history(NULL);
    while(ptr = readline("rl++:"))
    {
        add_history(ptr);
        line = ptr;
        cout << line << endl;
        free(ptr);
    }
    write_history(NULL);
}

ほぼ C 言語で使うときと一緒だが、実際的な処理は string 型に代入して扱った方が手軽だと思う。

C 言語版