I'm making a project in C++ and was tasked with replacing the std::getline with the more capable readline() function from the readline.h library since it conatins more functionalitites and will be more useful overall. However the program I am executing makes multiple tests with other "in-X" files and generates "out-X" files and checks output with result for testing.
The testing goes basically like this
./a.exe < in-X.txt > out-X.txt
The problem I'm having is the readline() function seems to print to the output file the line it read from the input, so basically all my "out-X" files generated contain all the input plus the expected result and that is very troublesome.
I've already tried many combinations of rl_redisplay() and using rl_delete_text() to no solution. I've also tried using terminal management functions such as rl_prep_terminal(1) to raw mode and rl_deprep_terminal() and nothing seems to work. I also couldn't try rl_tty_set_echoing() because my program doesn't seem to be able to find it in the library.
I've tried to minimize the issue as much as possible and came up with this simplified code
/* Standard include files. stdio.h is required. */
#include <string.h>
#include <iostream>
#include <stdlib.h>
/* Standard readline include files. */
#include <readline/readline.h>
#include <readline/history.h>
int main(int argc, char* argv[]) {
char *buf;
while ((buf = readline("Prompt>")) != nullptr) {
std::string line(buf);
if (buf)
add_history(buf);
std::cout << "Line : " << line << "\n";
}
return 0;
}
Which is very straightforward, printing the prompt of readline() is not an issue as it is expected on the output, but printing the "buf" that was read is.
Basically with the in file
a = 2
b = 3
1 + 2 + 3
I would expect the output
Line : a = 2
Line : b = 3
Line : 1 + 2 + 3
But I get
Prompt> a = 2
Line : a = 2
Prompt> b = 3
Line : b = 3
Prompt> 1 + 2 + 3
Line : 1 + 2 + 3
Prompt>
The last "Prompt>" that contains the EOF is something I can work around or live with, but printing the 1st 3rd and 5th lines I cannot.