Page 1 of 2

C++ getline long input problem

Posted: Sat Jun 02, 2018 10:57 am
by phhnguyen
Just converted my chess engine from XB to UCI protocol. However when testing manually on Terminal (MacOS) the function getline doesn't accept the lines longer than 1024 characters. Clearly it is not enough for long games.

Is there anything wrong (setup) here? Solution? Thanks.

The below code I have used to check the limit of the function getline:

Code: Select all

    std::string str;
    while(true) {
        if (!getline(std::cin, str))
            break;
        std::cout << "str length: " << str.length() << std::endl;
    }

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 11:17 am
by syzygy
Are you sure it is not a limit of the MacOS Terminal program or of the way you are trying to input a long line? If a newline somehow creeps into what you are inputting (e.g. by copy and paste), then obviously getline() will stop there.

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 1:25 pm
by phhnguyen
I can enter a very long command (over 1024 characters) into Terminal (not inside an app).

However when my program runs, it stops receiving at 1024th character for both ways: copy/paste and enter manually all characters.

The problem is I cannot enter positions in fast and natural way to debug.

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 1:59 pm
by Ras
I think it is the terminal driver. Try to generate a text file with your whole command sequence, i.e. starting with the initial "uci" and "isready". Then add a third line with line length > 1024. Generate debug output in your engine that tells the line length. And then pipe that file into the engine using cat, i.e. "cat ./myfile.txt | ./my_engine".

If it is the terminal, you will see a line length > 1024 in your engine.

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 2:02 pm
by syzygy
Try

Code: Select all

$ echo very long line
and see if you can make it output lines longer than 1024 characters. Probably not.

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 2:07 pm
by xr_a_y

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 2:31 pm
by phhnguyen
I did, it (echo in command line) works well with much longer 1024 characters.

I have created a simple program, compile with MS Visual studio and run with Win 10. The limit is much longer (4096) but still reachable.

Can someone help to test it? Just run below code, copy and paste any string, repeat until the program refuses to show more and then press enter, you will see the length of the string the getline function accepted:

Code: Select all

#include <iostream>
#include <sstream>

void main() {
	std::string str;
	while (true) {
		if (!getline(std::cin, str))
			break;
		std::cout << "str length: " << str.length() << std::endl;
	}
}


Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 2:48 pm
by phhnguyen
xr_a_y wrote:
Sat Jun 02, 2018 2:07 pm
This may help ? : https://stackoverflow.com/questions/143 ... bash-shell
Wow, thanks. The solution works!

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 2:51 pm
by syzygy
Instead of fighting the limits of the Terminal program, just write another program that outputs a string of a particular length and pipe it to your test program.

Re: C++ getline long input problem

Posted: Sat Jun 02, 2018 3:05 pm
by phhnguyen
Oops, are you sure you are giving suggestions to lazy-lack-skill people like me? :shock:

Creating one more program and / or programming with pipe techniques obviously is not easy and quick tasks.

The above solution requires me to copy / paste only few code lines. I have added already some defines to turn it on/off when need.