trivial
trivial

Reputation: 141

How to extract code comments from a C file?

I have a C file which contains lots of code comments, how I can I extract the comments? Just want the comments, not the c code. Is there any tools i can use to do this. e.g Doxygen? or need i to build a parser by myself? if needed, which language is better?

Thanks in advance.

thank you all!! i start to look into perl now, have never used this kind of language before, it seems so clever~~ and is there any suggestion for extracting xml from a file that contains xml?

Upvotes: 4

Views: 4283

Answers (2)

user339222
user339222

Reputation:

I suggest you to use an scripting language, such as Perl, and parse the file looking all the possibilities:

 - "//" for single-line comments.
 - "/*" and "*/" for multi-line comments.
 - doxygen ones "///", "/**", "/*!"

Upvotes: 0

mzet
mzet

Reputation: 585

Following one-liner displays all commented lines (or lines that contain comments) in main.c file:

cat main.c | awk '/\/\// {print $0}; /\/\*/ {blk=1}; {if(blk) print $0}; /\*\// {blk=0}'

Upvotes: 5

Related Questions