Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> How To Remove The ^m In Cpp Files ?, A^M at the end of each line in a source
jedipi
post Aug 20 2005, 04:32 AM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 352
Joined: 2-March 05
From: Australia
Member No.: 2,859



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.

This post has been edited by microscopic^earthling: Aug 21 2005, 02:50 AM
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post Aug 20 2005, 04:41 AM
Post #2


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411
myCENTs:84.36



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
Go to the top of the page
 
+Quote Post
saga
post Aug 20 2005, 08:34 AM
Post #3


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 68
Joined: 23-May 05
Member No.: 5,355



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 smile.gif how stupid of me smile.gif
Go to the top of the page
 
+Quote Post
qwijibow
post Aug 20 2005, 09:00 AM
Post #4


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,366
Joined: 14-September 04
From: Nottingham England
Member No.: 570



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 ???
Go to the top of the page
 
+Quote Post
yordan
post Aug 20 2005, 10:27 AM
Post #5


Way Out Of Control - You need a life :)
Group Icon

Group: [MODERATOR]
Posts: 2,183
Joined: 16-August 05
Member No.: 7,896
myCENTs:15.50



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.cpp

You 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
Go to the top of the page
 
+Quote Post
qwijibow
post Aug 20 2005, 09:56 PM
Post #6


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,366
Joined: 14-September 04
From: Nottingham England
Member No.: 570



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...

CODE

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



Attached File(s)
Attached File  dos2unix.c.txt ( 15.19k ) Number of downloads: 19
Attached File  dos2unix.h.txt ( 6.59k ) Number of downloads: 24
 
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Renaming collection of Pictures (files)!(15)
  2. How To Remove Bad Sectors Or Bad Clusters From HDD(18)
  3. Converting Flash Files To Gif(7)
  4. How To Hide Your Files In XP(18)
  5. Msn Contacts Removal(11)
  6. Open Source Software(4)
  7. How To Transfer Files From One Computer To Another(16)
  8. CS: Source Vs CS 1.6(50)
  9. VB.NET: Howto Add And Delete Files(9)
  10. Sharing Files In Windows Xp Home(15)
  11. How To Play *.rm Files With Media Player(14)
  12. Renaming Files (Using Excel Spreadsheet)(21)
  13. How Can I Delete Old Files In Windows Xp ?(22)
  14. Counter-Strike Source Clans - Post Your Server IPs(18)
  15. Get User Input From Vbscript For Batch Files(2)
  1. How To Embed Swf Files In Joomla ?(10)
  2. Need Help - How To Remove Session ID From URL(6)
  3. Creating Executable Jar Files(9)
  4. How To Open .bam Files !(5)
  5. Remove Sound From Avi(10)
  6. Qupis : Free Cpanel Web Hosting (one Line Text Ad At Bottom)(10)
  7. If You Have Some Private Files(17)
  8. Friends Can't Start The Exe Files I Send :((5)
  9. Make It Impossible To View Page Source(11)
  10. Need Some Help/advice On How To Restore Lost Files(5)
  11. Remove Windows Update Uninstall Information(4)
  12. Playing Two Wav Files Simultaneously In C#(4)
  13. Remove Drm From Itunes Aac M4v M4p On Win And Mac(1)


 



- Lo-Fi Version Time is now: 20th November 2008 - 08:05 PM