19

in a C++ program, there is a point when it reads a string like:

"NONAME_1_1\r" 

the \r is causing me trouble. I guess it prints or adds something like "^M". Is it right? Anyway it casues me problem, and I want to get rid of it.

I can not modify the input. I wonder how could I at this point, using C++, and in the easiest way, to remove \r for this string.

I know how to do it on bash but no clue on C++.

Thanks.

3
  • 1
    How do you read the string? Are you using iostreams? Please post the relevant code. Commented Mar 27, 2010 at 10:58
  • This sounds like a typical usage for strtok, if you were programming in C. Commented Mar 27, 2010 at 11:08
  • just a last question, when comparing strings character to character, I thought that "\r" would take two chars, but not, it is just one. how would i use a conditional ? if char[4]==`\r' ??? Commented Mar 27, 2010 at 15:25

4 Answers 4

49

I'm assuming that by string, you mean std::string.

If it's only the last character of the string that needs removing you can do:

mystring.pop_back(); 

mystring.erase(mystring.size() - 1); 

Edit: pop_back() is the next version of C++, sorry.

With some checking:

if (!mystring.empty() && mystring[mystring.size() - 1] == '\r') mystring.erase(mystring.size() - 1); 

If you want to remove all \r, you can use:

mystring.erase( std::remove(mystring.begin(), mystring.end(), '\r'), mystring.end() ); 
Sign up to request clarification or add additional context in comments.

3 Comments

just a last question, when comparing strings character to character, I thought that "\r" would take two chars, but not, it is just one. how would i use a conditional ? if char[4]==`\r' ???
@Werner: Backslash introduces and escape sequence. \r is a carriage return, ASCII code decimal 13/ hex 0D, ^M, etc. In single quotes it is a single character constant; in double quotes it would be a string literal composed of two characters: ASCII 13 and a terminating null.
Thank you for this post. I knew there had to be an easy way to clean up file lines from old mac and windows file encoding. So now I run getline(File, line) first with no third arg to remove the \n if it exist, then I use your example to clean off any \r. It seems to be working. thanks. without this, garbage was being left in the last index of my token vector of each loop. I'm sure it was the encoding \r
1

This is a common problem when reading lines from files after moving them between unix and windows.

  • Unix variants use "\n" (line feed) to terminate lines.
  • Windows uses "\r\n" (carriage return, line feed) to terminate lines.

You can run the "dos2unix" or "unix2dos" to convert lines in a file.

1 Comment

Then use Charles Bailey's solution.
0

That depends on how you are holding it in memory:

  1. If it's in a std::string, just check the last byte and remove it if it's a '\r'.
  2. If it's in a const char*, you can use strncpy to copy the string into another char array, conditionally grabbing the last byte.
  3. If, by "I can not modify the input," you simply mean that you can't touch the source file, then you may have the option to read it into a char array and replace any trailing '\r' with '\0'.

2 Comments

i see. then is it that easy just like detecting \r ? could you tell me how would you then do it? thanks a lot
@Werner, I don't know how you are representing the input. Some sample code in your question would help.
0

copy_if is useful if you want to leave the original string untouched and also if you want to want remove multiple characters like CR & LF in this example.

const std::string input = "Hello\r\nWorld\r\n"; std::string output; output.reserve(input.length()); std::copy_if(input.begin(), input.end(), std::back_inserter(output), [] (char c) { return c != '\r' && c != '\n'; }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.