[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6857
  • Last Modified:

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

  • 5
  • 4
1 Solution
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?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

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!

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now