0

OK so I've almost completed a program. However whilst it works on Windows I would prefer to run it on my Mac to test differences in performance (my Mac has much faster hardware).

I have an unordered map that is storing in values from a text file and I am also copying this map to reverse the key/value pairs.

The text files keep adding a new line, and from research I've found it to be because Windows adds it's own carriage return (why?!) and it's at the end of every second element in my map. The file is "stringx,stringy" and so am using stringstream to split the string x and y into the key/value pair.

EDIT: thanks for the answers guys, worked a treat!

3
  • Windows uses /r/n as the line break indicator because it has always done it historically. Changing it would break god knows how many lines of legacy code. Sad state of affairs really. Commented Jan 30, 2013 at 0:08
  • 1
    Unless you've got a requirement to store multi-line strings, simply remove both the \r and the \n at the end of the line. When you open the file, do you specify binary or text? If text, CRLF to NL mapping should be automatic. Commented Jan 30, 2013 at 0:08
  • If the file isn't opened in binary mode (that is DOESN'T have a 'b' on the mode, or ios::binary), you should not have any '\r' in your input. Try printing the input as hex-numbers [and check that you are not opening the file as binary!] Getline should not return you a string with newline in it anyways! I suspect your problem is in some other part of the code! Commented Jan 30, 2013 at 0:13

4 Answers 4

2

That isn't how std::string::replace works, you should read up on how it works here.

In order to do a basic replace, you could write your own function to do it, however in your case it seems to be a trimming issue since the carriage return is usually on the right side of the string.

You can remove the carriage return and new line by doing something like this:

std::string& rtrim(std::string& str) { size_t endpos = str.find_last_not_of("\r\n"); if(endpos != std::string::npos) { str.substr(0,endpos+1).swap(str); } return str; } 
Sign up to request clarification or add additional context in comments.

Comments

1

On some implementations, like Windows, using a read mode of "r" or a write mode of "w" will cause "\r\n" to be read/written when you meant to pass "\n" through. Use "wb" or "rb". For iostream functions, I believe you need to pass in the ios::binary flag.

Comments

0

Windows uses "\r\n" to end lines. Usually programs that are supposed to run on various platforms use some #ifdef to handle similar differences.

Comments

0

I think I understand what the question is now. It's not about dealing with the differences in code - you are actually trying to use a "DOS/Windows" file on a non-Dos/Windows machine - you need to use dos2unix to fix up the end of lines on your file!

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.