C++ getline long input problem

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
phhnguyen
Posts: 1494
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

C++ getline long input problem

Post 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;
    }
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
syzygy
Posts: 5647
Joined: Tue Feb 28, 2012 11:56 pm

Re: C++ getline long input problem

Post 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.
User avatar
phhnguyen
Posts: 1494
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: C++ getline long input problem

Post 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.
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
Ras
Posts: 2579
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: C++ getline long input problem

Post 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.
Rasmus Althoff
https://www.ct800.net
syzygy
Posts: 5647
Joined: Tue Feb 28, 2012 11:56 pm

Re: C++ getline long input problem

Post 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.
User avatar
xr_a_y
Posts: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Re: C++ getline long input problem

Post by xr_a_y »

User avatar
phhnguyen
Posts: 1494
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: C++ getline long input problem

Post 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;
	}
}

https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
User avatar
phhnguyen
Posts: 1494
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: C++ getline long input problem

Post by phhnguyen »

xr_a_y wrote: Sat Jun 02, 2018 4:07 pm This may help ? : https://stackoverflow.com/questions/143 ... bash-shell
Wow, thanks. The solution works!
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager
syzygy
Posts: 5647
Joined: Tue Feb 28, 2012 11:56 pm

Re: C++ getline long input problem

Post 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.
User avatar
phhnguyen
Posts: 1494
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

Re: C++ getline long input problem

Post 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.
https://banksiagui.com
The most features chess GUI, based on opensource Banksia - the chess tournament manager