C++ remove \n and \r (CRLF) from string

How do i remove CRLF from a string that i am inputting with getline(cin, commandStr);

I googled and it seems getline shouldn't be grabbing the \r\n but it is

when i do a cout << "aaaa" << cmd << "ss";

it prints like this

ssaaDoctor Check-Out 1

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Getline will stop at a linefeed terminator and not read it; however, that is partly dependent on how you opened the file. Did you open it as text or binary? Can you show all the code please?


The delimiter character is delim for the first function version, and '\n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
W00dyW00dAuthor Commented:
The file is inputted like this:

./assignment3 < sampleInput.txt
evilrixSenior Software Engineer (Avast)Commented:
Can you show all the code please? I see, you're redirecting the file to stdin but I'd like to see exactly how you're reading it. Also, can you attach the test file as that might also make a difference?
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

W00dyW00dAuthor Commented:
sure here you go.  thanks for the help

int main()
	Clinic clinic;
	Interpret command;
	string commandStr;

	cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
	cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
	do {
		getline(cin, commandStr);
		cout << commandStr << " test " << endl;
		if (!command.getStop())
			cout << endl << clinic.getMessage() << endl;
		cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" ;
		cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

	} while (!command.getStop());
	return 0;


Open in new window

evilrixSenior Software Engineer (Avast)Commented:
Thanks... looking at it now, get back to you shortly.
evilrixSenior Software Engineer (Avast)Commented:
Ok, so this is what I think the issue is...

Your file contains CRLF line terminators but getline only knows about LF so it's reading and disgarding these leaving the CR at the end of the line. because you're reading it from stdin rather than as a file that has been explicitly opened as text it's not performing the standard CRLF to LF conversion that would happen.

So, to remove CR either open the file explicitly using fstream as a text file or add the following just after the getline.

std::remove(commandStr.begin(), commandStr.end(), '\r');

This will remove ALL CR from the string (this includes any embedded in the middle of the string).


if you just want to remove the last one do this...

if(*commandStr.rbegin() == '\r') { commandStr.resize(commandStr.size() - 1); }


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W00dyW00dAuthor Commented:
Okay that gave me an idea.  I had found this below but added it into my code later on which didnt solve the issue.  I tried yours right after the getline but it still seemed to do the same thing but adding in the erase worked.

std::remove(commandStr.begin(), commandStr.end(), '\r');


commandStr.erase(remove(commandStr.begin(), commandStr.end(), '\r'), commandStr.end());

Thanks for the help
W00dyW00dAuthor Commented:
Lead me in the right direction.
evilrixSenior Software Engineer (Avast)Commented:
Ah, yeah -- my bad -- oops :)

Remove returns an interator to the end of the new range, which you then pass to erase to remove the remaining part of the range. Darn, I ALWAY forget about that :)

"Return value: A forward iterator pointing to the new end of the sequence, which now includes all the elements with a value other than value."

Well spotted!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.