Strange cout behavior

I built a program on Ubuntu that was developed for (and works) Windows. On Ubuntu, I see this code:

string s = values_[9];
cout << s << endl;
cout << s << "x\n";

creating this conclusion:

high
xigh

The expected output for the second line is "highx". I know that the values_ [9] value is initially read from a file (written on Windows). Printing other lines seems to work fine.

What's going on here?

+5
source share
3 answers

Run the command with its exit through cat -A. Probably either the value sor the result created endlgives you a character '\r'that usually sends the cursor back to the beginning of the line.

: '\r' s, , endl. , - , s, "high\r", .

EDIT2: cat -A ( GNU), cat -v , , od -c.

+8

, , '\r'. , high, , . , x, .

(, dos2unix(1) ), .

+4

What is probably happening is what is \rin values_[9].

+3
source

All Articles