C++ getline long input problem

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
phhnguyen
Posts: 291
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

C++ getline long input problem

Post by phhnguyen » Sat Jun 02, 2018 10:57 am

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

syzygy
Posts: 4343
Joined: Tue Feb 28, 2012 10:56 pm

Re: C++ getline long input problem

Post by syzygy » Sat Jun 02, 2018 11:17 am

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.

phhnguyen
Posts: 291
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

Re: C++ getline long input problem

Post by phhnguyen » Sat Jun 02, 2018 1:25 pm

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.

Ras
Posts: 1018
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: C++ getline long input problem

Post by Ras » Sat Jun 02, 2018 1:59 pm

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: 4343
Joined: Tue Feb 28, 2012 10:56 pm

Re: C++ getline long input problem

Post by syzygy » Sat Jun 02, 2018 2:02 pm

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: 266
Joined: Sat Nov 25, 2017 1:28 pm
Location: France

Re: C++ getline long input problem

Post by xr_a_y » Sat Jun 02, 2018 2:07 pm


phhnguyen
Posts: 291
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

Re: C++ getline long input problem

Post by phhnguyen » Sat Jun 02, 2018 2:31 pm

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


phhnguyen
Posts: 291
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

Re: C++ getline long input problem

Post by phhnguyen » Sat Jun 02, 2018 2:48 pm

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!

syzygy
Posts: 4343
Joined: Tue Feb 28, 2012 10:56 pm

Re: C++ getline long input problem

Post by syzygy » Sat Jun 02, 2018 2:51 pm

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.

phhnguyen
Posts: 291
Joined: Wed Apr 21, 2010 2:58 am
Location: Australia
Full name: Nguyen Hong Pham
Contact:

Re: C++ getline long input problem

Post by phhnguyen » Sat Jun 02, 2018 3:05 pm

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.

Post Reply