拆下'//'&;/*…*/使用c获取预处理文件的c文件中的注释

拆下'//'&;/*…*/使用c获取预处理文件的c文件中的注释,c,comments,C,Comments,这是我的程序,我正在尝试删除单行和多行注释。。。 这里我使用命令行参数来执行程序 $。/a.out sample.c sample.i 我想删除c文件中的注释,并像预处理一样获得纯c文件。。 在执行我的程序时,它会给出分段错误,并且不会删除注释。。。 任何人都可以尝试一下,告诉我程序中的错误发生在哪里,以及如何避免 #include<stdio.h> #include<stdlib.h> #include<string.h> main(int argc,cha

这是我的程序,我正在尝试删除单行和多行注释。。。 这里我使用命令行参数来执行程序
$。/a.out sample.c sample.i
我想删除c文件中的注释,并像预处理一样获得纯c文件。。 在执行我的程序时,它会给出分段错误,并且不会删除注释。。。 任何人都可以尝试一下,告诉我程序中的错误发生在哪里,以及如何避免

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(int argc,char **argv)
{
    FILE *fp;
    char *s=NULL,*p=NULL,*q=NULL;
    int i,size,j;
    if(argc!=3)
    {
    printf("error : improper use of file\n");
    printf("use : ./file <filename.c> <filename.i>\n");
    return;
    }
    fp=fopen(argv[1],"r");
    if(fp==NULL)
    {
    printf("file does not exist\n");
    return;
    }
    fseek(fp,0,2);
    size=ftell(fp)+1;
    rewind(fp);
    s=calloc(1,size);
    fread(s,size-1,1,fp);
    fclose(fp);
    puts(s);
    p=s;
    for(i=0;s[i];i++)
    {
    if(p=strchr(p,'"'))
            {
            p++;
            while(!(p=strchr(p++,'"')));//with in printf("....// .... /*....*/ ..") the comment cant be removed
            i=p-s;
            printf("i=%d\n",i);
            puts(s);
            }
else if(p=strstr(p,"//"))
            {
            while(*p!='\n')
            *p++=' ';
            i=p-s;
            printf("i=%d\n",i);
            puts(s);
            }
    if(p=strstr(p,"/*"))
            {
            j=p-s;
            while(!(p=strstr(p,"*/")))
            p++;
            strcpy(s+j,p+2);
            i=p+2-s;
            printf("i=%d\n",i);
            puts(s);
            }
    }
    puts(s);
    fp=fopen(argv[2],"w");
    size=strlen(s)-1;
    fwrite(s,size,1,fp);
    fclose(fp);
}
#包括
#包括
#包括
主(内部argc,字符**argv)
{
文件*fp;
char*s=NULL,*p=NULL,*q=NULL;
int i,大小,j;
如果(argc!=3)
{
printf(“错误:文件使用不当”\n”);
printf(“使用:./file\n”);
回来
}
fp=fopen(argv[1],“r”);
如果(fp==NULL)
{
printf(“文件不存在\n”);
回来
}
fseek(fp,0,2);
尺寸=ftell(fp)+1;
倒带(fp);
s=calloc(1,尺寸);
fread(s,尺寸-1,1,fp);
fclose(fp);
卖出(s);
p=s;
对于(i=0;s[i];i++)
{
if(p=strchr(p,“”))
{
p++;
而(!(p=strchr(p++,““”);//在printf中(“../../../../../../../../../..”)则无法删除注释
i=p-s;
printf(“i=%d\n”,i);
卖出(s);
}
否则如果(p=strstr(p,“/”)
{
而(*p!='\n')
*p++='';
i=p-s;
printf(“i=%d\n”,i);
卖出(s);
}
如果(p=strstr(p,“/*”))
{
j=p-s;
而(!(p=strstr(p,“*/”))
p++;
strcpy(s+j,p+2);
i=p+2-s;
printf(“i=%d\n”,i);
卖出(s);
}
}
卖出(s);
fp=fopen(argv[2],“w”);
尺寸=strlen(s)-1;
fwrite(s,大小,1,fp);
fclose(fp);
}
这是我想从这个c文件中删除注释的示例.c程序

#include<stdio.h>//declaring stdio header file
#include<stdlib.h>/*declaring stdlib header file*/
main()//main function starts
{
    int a,b;//declaring integer variables
    printf("Enter // a & b:");
    scanf("%d%d",&a,&b);/* taking a & b from std i/p device */
    printf("/* result %d * %d = %d */\n",a,b,a*b);//printing result
}
#包含//声明stdio头文件
#include/*声明stdlib头文件*/
main()//主函数启动
{
int a,b;//声明整数变量
printf(“输入//a&b:”);
scanf(“%d%d”、&a和&b);/*从标准i/p设备获取a和b*/
printf(“/*结果%d*%d=%d*/\n”,a,b,a*b);//打印结果
}
我希望输出应该是

#include<stdio.h>
#include<stdlib.h>
main()
{
    int a,b;
    printf("Enter // a & b:");
    scanf("%d%d",&a,&b);
    printf("/* result %d * %d = %d */\n",a,b,a*b);
}
#包括
#包括
main()
{
INTA,b;
printf(“输入//a&b:”);
scanf(“%d%d”、&a和&b);
printf(“/*结果%d*%d=%d*/\n”,a,b,a*b);
}

当我对一个测试文件运行这个程序时,它会在线崩溃

if(p=strchr(p,'"'))
原因是如果没有找到匹配的字符串或字符,
strstrstr
amd
strstrstr
将返回
NULL
,因此您可能不想将结果分配回
p

一些NIT:

  • 没有理由将整个文件读入内存。这种方法无法扩展,只会增加更多出错的地方

  • 如前所述,此代码无法很好地处理嵌套注释(如果在
    /**/
    分隔块中嵌入了
    /
    分隔注释,会发生什么情况?)。它也不会处理转义的
    字符;例如,如果您有一个字符串,如
    “引号字符是\”“
    ”,则引号处理块将错误地假定第三个
    后面的所有内容都是字符串文字的一部分,而不是字符串文字的一部分

  • 尝试使用调试器(如GDB)并找出segfault发生的确切位置。这将帮助您找到需要修复的错误代码。此外,如果您修复缩进,人们将更有可能帮助您。难以阅读的代码将被关闭。