删除.c文件中的注释 [英] delete comments in .c file

查看:97
本文介绍了删除.c文件中的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除.c文件中的所有评论。


.c文件的大小非常大。


任何好主意要做这个吗?


请给我看一些示例代码。

解决方案

>我想删除.c文件中的所有注释。


.c文件的大小非常大。

这样做有什么好主意吗?



假设你有这样的C评论/ *,对吧? * /

您可以使用UNIX sed(流编辑器)。你不会相信这个,

但是:cat input.c | sed -e's / \ / \ *。* \ * \ /// g''> output.c


做差异以确保它正常工作。


-

Jem Berkes
http://www.sysdesign.ca/





Jem Berkes写道:

我想删除所有评论在.c文件中。

.c文件的大小非常大。

这样做有什么好主意吗?



假设你有这样的C评论/ *,对吧? * /
您可以使用UNIX sed(流编辑器)。你不会相信这个,
但是:cat input.c | sed -e's / \ / \ *。* \ * \ /// g''> output.c

做一个差异,以确保它正常工作。




这只有在所有评论都在你描述的形式,不可能是
。例如如果评论是这样的话,它就不会起作用:


/ *开始一个相当常见的表格

*的评论栏。

* /


或:


/ * set x:* / x = 7; / *现在更多的东西...... * /


在第一种情况下它不会删除评论而在第二次

不寻常的情况下它'将删除两条评论加上x = 7;;任务

他们之间。


你需要谷歌周围或如果你想要一个UNIX工具解决方案,发布这个

到一个UNIX NG(例如comp.unix.questions或comp.unix.shell)。


Ed。


Jem Berkes< je*@users.pc9__org>写道:

我想删除.c文件中的所有注释。

.c文件的大小非常大。

这样做有什么好主意吗?



假设你有这样的C评论/ *,对吧? * /
您可以使用UNIX sed(流编辑器)。你不会相信这个,
但是:cat input.c | sed -e's / \ / \ *。* \ * \ /// g''> output.c

做一个差异以确保它正常工作。




这不会检测到多行注释。它也无法正确地忽略字符串和字符文字中的注释分隔符。它会从第一个/ *中删除所有内容。在一行到最后一个* /在

同一行;例如,它改变了这个:

x = / *一条评论* / 42; / *另一个评论* /

到这个:

x =

而且它取而代之的是每个评论而不是空白,所以<有效的C片段后面的


x = sizeof / * comment * / int;

替换为:

x = sizeofint;


如果您正在处理C99代码,您将不得不担心//

评论(很多前-C99编译器支持这些作为扩展)。


剥离C注释比看起来要复杂得多;你差点

必须复制预处理器的大部分功能才能获得

吧。


我真的要问原始海报:你为什么要这样做?


-

Keith Thompson(The_Other_Keith) ks *@cts.com < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://www.sdsc.edu/~kst>

Schroedinger做莎士比亚:要*和*不要


I want to delete all comments in .c file.

Size of .c file is very big.

Any good idea to do this?

Please show me example code.

解决方案

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

--
Jem Berkes
http://www.sysdesign.ca/




Jem Berkes wrote:

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 would only work if all comments are in the form you describe, which
isn''t likely. e.g. it won''t work if the comments are:

/* Start of a fairly common form
* of comment block.
*/

or:

/* set x: */ x = 7; /* now more stuff... */

In the first case it won''t delete the comment while in the second
unusual case it''ll delete both the comments plus the "x = 7;" assignment
between them.

You need to Google around or if you want a UNIX tool solution, post this
to a UNIX NG (e.g. comp.unix.questions or comp.unix.shell).

Ed.


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_Keith) 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"


这篇关于删除.c文件中的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆