I am using std::getline in a while loop and printing the output with cout, I find that the beginnings of my line are being cut off when I am printing:
works as expected:
std::string line;
while(std::getline(csv, line)) {
std::cout << line << std::endl
}
doesn't work as expected, cuts the first two characters out of my line
std::string line;
while(std::getline(csv, line)) {
std::cout << line << " " << std::endl
}
I've never noticed this behaviour before, why is it happening now?
This is what seems to be happening:
The characters at he the end of your line
string contain carriage return
character. In case your string was Hello World
, printing it along with a carriage return would cause the cursor to go back at the H
character. Which means when you start printing anything after that, it will overwrite your old string.
To avoid this you may want to print endl
before anything else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With