Jem Berkes <je*@users.pc9_ _org> writes:
I want to delete all comments in .c file.
Size of .c file is very big.
Any good idea to do this?
Assuming you have C comments /* like this, right? */
You can use UNIX sed (stream editor). You're not going to believe this,
but: cat input.c | sed -e 's/\/\*.*\*\///g' > output.c
Do a diff to make sure it's working correctly.
That won't detect multi-line comments. It also fails to properly
ignore comment delimiters inside string and character literals. It
deletes everything from the first "/*" on a line to the last "*/" on
the same line; for example, it transforms this:
x = /* one comment */ 42; /* another comment */
to this:
x =
And it replaces each comment by nothing rather than by a blank, so the
following valid C fragment:
x = sizeof/*comment*/int;
is replaced with this:
x = sizeofint;
If you're dealing with C99 code you'll have to worry about "//"
comments (many pre-C99 compilers support these as an extension).
Stripping C comments is a lot more complex than it looks; you almost
have to duplicate most of the functionality of the preprocessor to get
it right.
I really have to ask the original poster: why do you want to do this?
--
Keith Thompson (The_Other_Keit h)
ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"