Jump to content
xisto Community
Sign in to follow this  
jedipi

How To Remove The ^m In Cpp Files ? A^M at the end of each line in a source

Recommended Posts

My friend just sent me a program source file.It is a *.cpp file (C++ source).When I open with text editor (gedit), there is a ^M at the end of each line.Then I try vim and emacs. they just give me a same result.What is that ^M???Why does it there???How can I delete them all (not one by one)???Thank you.

Edited by microscopic^earthling (see edit history)

Share this post


Link to post
Share on other sites

I've seen that before and as far as I know - all the ^M-s represent the CR-LF combination of DOS. In DOS/Windows, a line is terminated by Carriage Return (CR) and Line Feed (LF) - whereas in *NIX platforms, a CR is enough to bring the cursor to the next line. I think it's this discrepancy that renders the CR-LF combination as a ^M in gedit/vim etc. You can safely remove them. Simply do a mass search and replace and replace the ^M with a space. that'll be easier than deleting them from the end of each line manually.Regards,m^e

Share this post


Link to post
Share on other sites

Just a thought..What if after deleting those escape sequences (are they escape sequence?).....ahhhhhh nevermind, yeah it wouldnt matter even if the compiler when parsing the code would ommit CR-LF becuase a statement is always terminated by semi-colon :D how stupid of me :D

Share this post


Link to post
Share on other sites

In Kedit (the KDE version of GNOME's Gedit, you can select MS-DOS character encoding.can Gedit do the same ?There is a command line tool that converts dos txt files to unix, but i just catn remember its name.. anyone ???

Share this post


Link to post
Share on other sites

Keep cool, man, most of the compilers simply ignore that ^M thing. It should only be a slight lack of comfort for you.Here is how to make the mass substitution using vi (sorry, vi is very basic but some people still use it).First open your file using vi. In a Unix window, type :vi myfile.cppYou will see the vi screen, not very comfortable but let's use it.Type type following sequence. the sequence starts with (Esc) which is my way of saying that you must press the Escape key (at the upper left of your keybord). the escape key tells vi that you are entering the command mode, instad of the text browsing mode. type the following :(Esc) :%s/(Ctrl-V)(Ctrl-M)//(finishing with Enter, of course).So, here :(Ctrl-V) means, press and keep pressing the Ctrl key while pressing the v key.(Ctrl-M) means, "press and keep pressing the control key while pressing the m key".in the "%s" string, s means "Substitue" and "%" means "in the whole document)./first/second/ means replace "first" by "second".(Ctrl-V) means "what follows is a control sequence, accept it with executing it"(Ctrl-M) is the control sequence for "next line"so /(Ctrl-V)(Ctrl-M)/ which is the ^M you don't like, will be replaced with //, whic his nothing.This is a very long explanation for the lot of things which will be done by the simple command line. Don't really worry about that long explanation, simply type my line :(Esc) :%s/(Ctrl-V)(Ctrl-M)//You will see all the ^M disappear. Then save your text and exit by typing(Esc) :wq(wq means : write and quit).Hope this helped

Share this post


Link to post
Share on other sites

AHAAAAAAA.

found it, the tool to convert dos text files to unix is called..... "dos2unix"
it will probably be installed with your distro, if not i have attached th source code to this post.

install it like so...

gcc -O2 ./dos2unix.c -o ./dos2unixsu<root password>mv ./dos2unix /usr/bin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.