删除代码中的注释

#define N 1024
void delete_notes(char infilename[],char outfilename[])
{
    FILE* infp = fopen(infilename, "r+");
    FILE* outfp = fopen(outfilename, "w+");

    int flag = 0, flag1 = 1;
    char ch, nch, str[N];
    while(!feof(infp))
    {
        ch = fgetc(infp);
        if(!flag)
        {
            if(ch == '/')
            {
                nch = fgetc(infp);
                if(nch == '*')
                    flag = 1;
                else if(nch == '/')
                {
                    flag1 = 0;
                    fgets(str,N,infp);
                    fputs("\n",outfp);
                }
                else
                {
                    ungetc(nch,infp); //若还不是注释,退格一个字符
                }
            }
        }
        if((!flag) && flag1)
        {
            if(ch!= EOF)
            fputc(ch, outfp);
        }
        flag1 = 1;
        if(flag)
        {
            if(ch == '*')
            {
                nch = fgetc(infp);
                if(nch == '/') //出现了“*/”,注释结束
                {
                    flag = 0;
                }
                else
                {
                    ungetc(nch, infp); //若还是注释,则要退格一个字符
                }
            }
        }
    }
    fclose(infp);
    fclose(outfp);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值