PDA

View Full Version : C++ and Readline



pianoboy3333
April 10th, 2006, 08:19 PM
What's with readline, I can't seem to get it to work, here's what I tried:



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

int main(){

char asdf=readline("# ");

return 0;
}

rplantz
April 10th, 2006, 08:44 PM
What's with readline, I can't seem to get it to work, here's what I tried:



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

int main(){

char asdf=readline("# ");

return 0;
}


readline returns a char *. Your variable, asdf, is a char. Don't forget to allocate the memory that your char * points to before you call readline.

pianoboy3333
April 10th, 2006, 08:56 PM
readline returns a char *. Your variable, asdf, is a char. Don't forget to allocate the memory that your char * points to before you call readline.

so use char * asdf=readline("# "); ? How do I allocate memory?

pianoboy3333
April 10th, 2006, 09:16 PM
When I do char * asdf=readline("# "); I get the error:



/tmp/ccIv9qtJ.o: In function `main':test.cc:(.text+0x24): undefined reference to `readline'
collect2: ld returned 1 exit status

rplantz
April 11th, 2006, 04:14 AM
Okay, I take back what I said about having to allocate space for the text string. I've never used readline before, and some exploration indicates that it allocates the space itself. You definitely want to check this out!

I had to install libreadline5-dev, which I did using synaptic. It included libncurses5-dev as a dependency.

Next, I had to specify the readline library when compiling with the -lreadline option:

$ g++ -lreadline readln.cc


Here's the program I used to test this:

// added memory deallocate -- 4/12/06
#include<stdio.h>
#include<readline/readline.h>
#include<readline/history.h>

int main(){

char* asdf = readline("# ");
printf("You entered: %s\n", asdf);
free(asdf);
return 0;
}


Again, I've never used readline in a program, so please double-check everything I've said here. I did not test my program to see if readline works as advertised.

From the very little I read about readline while figuring this out, it looks interesting. I learned something new today. :) I would appreciate any pointers to places where I can learn more about readline.

tomchuk
April 11th, 2006, 04:34 AM
EDIT: Oops, beaten to the punch.
rplantz - the readline info page is a pretty good source: lots of info, examples, etc.

Don't worry about allocating memory, from readline(3):


The line returned is allocated with malloc(3); the caller must free it when finished.


foo.c:


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

int
main ()
{
char *foo = readline("# ");
printf("%s\n", foo);
return 0;
}


compile with:


gcc -Wall -lreadline -ofoo foo.c

thumper
April 11th, 2006, 08:35 AM
Why is the post topic C++ and the examples all C?

If you are looking for a C++ way, then don't use readline.

pianoboy3333
April 11th, 2006, 02:54 PM
Why is the post topic C++ and the examples all C?

If you are looking for a C++ way, then don't use readline.

What do you suggest I use then?

*EDIT* rplantz example works fine....

thumper
April 11th, 2006, 03:14 PM
#include <string>
#include <iostream>

int main()
{
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit")
{
std::cout << "Bye\n";
break;
}
else std::cout << "echo: " << line << '\n';
}
}


Caveat: untested code.

LordHunter317
April 11th, 2006, 03:20 PM
Caveat: untested code.It's uncomparable anyway. readline() does infinitely more than read a line, it's the command-line editing library most code applications use.

Now, to be fair, if you're jsut using it for the former application, thumper's code works fine. If you're not, there is no equivalent C++ library I'm aware of, nor would you want to write one.

rplantz
April 11th, 2006, 04:10 PM
This is very cool. After reading very little of the info on readline, it was very simple to modify my program so that I could maintain a history of previously entered lines. The other thing I realized is that I can edit a line using the arrow keys instead of deleting characters and typing them over again.

I have to admit that my initial response to pianoboy3333's question was "dumb question" and I almost gave a response like thumper's. But I was a CS professor for 21 years, where I learned a lot from my students' "dumb questions." First, they asked things that I didn't even know about. (Such is the case here. I had never heard of readline before.) Second, I had to then learn about the topic in order to avoid embarrassing myself in front of an entire class of students. (Still did that a few times. :) )

Thanks for raising the question and for the responses.

pianoboy3333
April 11th, 2006, 05:01 PM
#include <string>
#include <iostream>

int main()
{
std::string line;
while (std::getline(std::cin, line))
{
if (line == "quit")
{
std::cout << "Bye\n";
break;
}
else std::cout << "echo: " << line << '\n';
}
}


Caveat: untested code.

This would have been my last resort, but with readline() you can also interact with things such as Ctrl-T to do something...

A fine example is at http://piano.juicemedia.tv/rlexample.cc
Compile it with `g++ rlexample.cc -o rlexample -lreadline'

thumper
April 12th, 2006, 09:23 AM
None of the examples (above, or the example posted above) actually free the memory that readline allocates.

According to the man page readline mallocs the memory and it is up to the caller to free it. Make sure you do. Assigning it to a string does not free the memory.

rplantz
April 12th, 2006, 05:08 PM
None of the examples (above, or the example posted above) actually free the memory that readline allocates.

According to the man page readline mallocs the memory and it is up to the caller to free it. Make sure you do. Assigning it to a string does not free the memory.

Thanks. I saw that later when I read more on the man page. I added the free() to my very simple example above.