Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: C++ and Readline

  1. #11
    Join Date
    Jul 2005
    Location
    Northern CA
    Beans
    657
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: C++ and Readline

    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.
    Intel i7-920; Nvidia GT 220, 1GB; MSI X58 Pro-E; 6GB DDR; 64-bit mode.

  2. #12
    Join Date
    Jan 2006
    Beans
    173
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: C++ and Readline

    Quote Originally Posted by thumper
    Code:
    #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'
    pianoboy3333

  3. #13
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: C++ and Readline

    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.
    ACCU - for programmers who care

  4. #14
    Join Date
    Jul 2005
    Location
    Northern CA
    Beans
    657
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: C++ and Readline

    Quote Originally Posted by thumper
    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.
    Intel i7-920; Nvidia GT 220, 1GB; MSI X58 Pro-E; 6GB DDR; 64-bit mode.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •