去除注释C代码

#include <stdio.h>


void delNote(FILE *fp,FILE *fp2);
int main(int argc, const char *argv[])
{
FILE *fp,*fp2;
fp=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
delNote(fp,fp2);
fcloseall();
return 0;
}


void delNote(FILE *fp,FILE *fp2)
{
  enum
  {
  normal,
  single,
  multiple,
  string,
  }mode=normal;


  char lead,follow=0;


  while((lead=fgetc(fp))!=EOF)
  {
      switch(mode)
  {
       case normal:
{
if (follow == '/')
{
if (lead == '/')
{
    mode = single;

else if (lead == '*') 
{
    mode = multiple;
}
else 
{
   fputc(follow,fp2);
   fputc(lead,fp2);
}
}
else if (lead != '/')
{
fputc(lead,fp2);
if (lead == '"') 
{
   mode = string;
}
}
}break;


case single:
{
if (follow != '\\' && (lead == '\n' || lead == '\r'))
{
fputc(lead,fp2);
lead = 0;
mode = normal;
}
} break;


case multiple: 
{
if (follow == '*' && lead == '/')
{
lead = 0;
mode = normal;
}
} break;


case string: 
{
if (follow == '\\')
{
fputc(follow,fp2);
fputc(lead,fp2);

else if (lead != '\\') 
{
    fputc(lead,fp2);
if (lead == '"')
{
mode = normal;
}
  }
} break;




}
follow = lead;
}
}
    

C代码注释删除工具,用Lex生成。可删除C源程序中的/* xxxx */的块或单行注释,及C++风格的单行注释,对于常量字符串中出现的注释不清除。文件包含Lex代码及编译的Linux下的可执行文件。编译及使用见Readme。 C注释的类型如下 // /* // * "C-style block comment be removed // * C-style block comment be removed" // * C-style block comment be removed // */ // // /* C-style single line comment be removed */ // // // c++ comment be removed // // ///* c++ comment be removed */ // c++ comment be removed // // // c++ comment enclosed with double-quote untouched // "////*\"c++ comment enclosed with double-quote untouched\" ##*/\n" // // // c-style single line comment enclosed with double-quote untouched // "/*c-style multiple line comment enclosed with double-quote untouched*/\n" // // // c-style multiple line comment enclosed with double-quote untouched // "/** c-style multiple line comment enclosed with double-quote untouched \n\ // \n next line\ // */\n" 示例文件: int main() { /* * "C-style block comment be removed * C-style block comment be removed" * C-style block comment be removed */ /* C-style single line comment be removed */ int foo; /* C-style single line comment be removed */ /* C-style single line comment be removed */ int bar; int foobar; /* "C-style block comment be removed C-style block comment be removed C-style block comment be removed */ // c++ comment be removed ///* c++ comment be removed */ // c++ comment be removed // "////" ???? // "/*test file */" ????? /* ssss /* dddd */ printf("////*\"c++ comment enclosed with double-quote untouched\" ##*/\n"); // c++ comment enclosed with double-quote untouched printf("/*c-style multiple line comment enclosed with double-quote untouched*/\n"); // c-style single line comment enclosed with double-quote untouched // printf("/** test cmnt \n //\n ss //*/"); printf("/** c-style multiple line comment enclosed with double-quote untouched \n\ \n next line\ */\n"); // c-style multiple line comment enclosed with double-quote untouched } 注释删除后成为: int main() { int foo; int bar; int foobar; printf("////*\"c++ comment enclosed with double-quote untouched\" ##*/\n"); printf("/*c-style multiple line comment enclosed with double-quote untouched*/\n"); printf("/** c-style multiple line comment enclosed with double-quote untouched \n\ \n next line\ */\n"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值