Reputation: 1089
Alright, I'm attempting to remove these pesky ^M characters that pop up when I compile and run my program on Linux.
I've attempted running
dos2unix filename
on the file, and the ^M's remain. I've also made sure that anytime I am opening the file, I am opening with
ios::binary
Is there some way to remove the M's? Even a system call would work that I could call within my code would be fine as well, something like
std::system("Remove M's Command");
Any feedback would be most appreciated.
Thanks.
Upvotes: 1
Views: 3500
Reputation: 168726
If your C++ program is already structured like this:
std::string str;
std::ifstream inputFile("file.txt", ios::binary);
while (std::getline(inputFile, str)) {
// parse str and operate on the results
}
Then you can easily change it to:
std::string str;
std::ifstream inputFile("file.txt", ios::binary);
while(std::getline(inputFile, str)) {
str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
// parse str and operate on the results.
}
Upvotes: 1
Reputation: 16441
I'm not sure what ios::binary
is, but it seems to me like the key.
It sounds related to Apple systems, and they use the CR (^M) character instead of LF (while Windows uses both). So if you have only CR, not CR-LF, then dos2unix won't work.
So why not just remove ios::binary
(as @Joachim-Isaksson suggested)?
Upvotes: -1
Reputation: 22262
A number of tools will do it with a regular expression. For example, perl can edit the file in place:
# perl -i -p -e 's/\r//g' FILENAME
Upvotes: 6