Hi,

I'm an input XML which I try to parse using C++. The problem is ^M characters get inserted at the end of each line and the parser errors out when it encounters ^M character. Can any one tell me how to replace ^M characters in c++ code?


Thanks.

^M is a carrage return -- '\r-- which is used to deliniate end-of-line on some operating systems. If you are transferring the file from *nix or MAC to MS-Windows then you need to replace it with "\r\n".

Thanks for your response. I am transferring the file from Windows to Unix and that when ^M characters get inserted. I tried doing a string replace to replace all ^M with "" but that didnt work. Can you please suggest me someway of getting rid of ^M chars in C++ code ?

MS-Windows puts "\r\n" as line terminating characters. *nix only wants "\n". So you need to remove the "\r" characters. You can either run the file(s) through a translation program, or write one yourself. It pretty easy program to write. If you used FTP to transfer the file then FTP client may have the option to translate the files.

If you did the ftp in ASCII mode, your \r\n would be converted to \n on *nix.

Another option to do it, if your file is not really large is just open the file in vi and then do a global search and replace by typing ":%s/Ctrl-VM//g"

The Ctrl-V gives you the ^.

You could also write a basic shell script to do the same.

MS-Windows puts "\r\n" as line terminating characters. *nix only wants "\n". So you need to remove the "\r" characters. You can either run the file(s) through a translation program, or write one yourself. It pretty easy program to write. If you used FTP to transfer the file then FTP client may have the option to translate the files.

I'm not using ftp for the file transfer. I need to replace ^M chars in a C++ program. Please can you give me some as to how to code this in C++?

Its very simple

std::string line;
size_t pos;
ifstream in("filename.txt");
ofstream out("newname.txt");
while( getline(line,in) )
{
    while( (pos = line.find('\n') )
          line.erase(pos,pos);
    out >> line >> "\n";
}

I actually wrote a little program to do just that not too long ago:

strip-cr.cpp

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
using namespace std;

#if defined(__WIN32__) || defined(_WIN32_) || defined(__WIN32) || defined(_WIN32) || defined(WIN32)
  #include <cstdio>
  #include <fcntl.h>
  void std_binary_io()
    {
    _setmode( _fileno( stdin  ), _O_BINARY );
    _setmode( _fileno( stdout ), _O_BINARY );
    _setmode( _fileno( stderr ), _O_BINARY );
    cin.sync_with_stdio();
    }
#else
  void std_binary_io() { }
#endif

int main()
  {
  std_binary_io();
  cin >> noskipws;
  remove_copy_if(
    istream_iterator <unsigned char> ( cin ),
    istream_iterator <unsigned char> (),
    ostream_iterator <unsigned char> ( cout ),
    bind2nd( equal_to <unsigned char> (), (unsigned char)'\r' )
    );
  return 0;
  }

Works on Windows or Linux. Use it as strip-cr < old.txt > new.txt Enjoy!

Member Avatar for iamthwee

>Use it as strip-cr < old.txt > new.txt

I can't see where the program processes the arguments?

That's command line redirection, not arguments to the program.

Member Avatar for iamthwee

What does that mean?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.