Re: delete comments in .c file
Jeremy Yallop wrote:[color=blue]
>
> Tim Hagan wrote:[color=green]
> > Jeremy Yallop wrote:[color=darkred]
> >>
> >> Tim Hagan wrote:
> >> > This will replace each /* ... */ style comment with a single space:
> >> >
> >> > #include<stdio. h>
> >> > int main(void){i\
> >> > nt c,p=-1,k=0,s=0
> >> > ;while((c=getch ar
> >> > ())!=EOF){if(s= =0
> >> > ){if(p=='/'&&c==
> >> > '*'){s=1;k=2;}e l\
> >> > se if(c=='\"'&&p
> >> > !='\\'&&p!='\'' )s
> >> >=2;}else if(s==1)
> >> > {if (p=='*'&&c==
> >> > '/')s=0;}else if(
> >> > s==2){if(c=='\" '
> >> > &&p!='\\')s=0;} if
> >> > (k==1)putchar(' '
> >> > );if(p>0&&s!=1) {
> >> > if(!k)putchar(p );
> >> > if(--k<0)k=0;}p=c
> >> > ;}putchar(p);re t\
> >> > urn 0;}
> >>
> >> It doesn't handle line-splicing. Also, putchar(-1) is not portable.[/color]
> >
> > putchar(-1) is never executed in the above code[/color][/color]
.... unless one tries to remove the comments from an empty file. :-)
[color=blue]
> putchar(-1) is executed if EOF is encountered immediately.[/color]
So just insert 'if (p > 0)' before the final putchar.
--
Tim Hagan
Jeremy Yallop wrote:[color=blue]
>
> Tim Hagan wrote:[color=green]
> > Jeremy Yallop wrote:[color=darkred]
> >>
> >> Tim Hagan wrote:
> >> > This will replace each /* ... */ style comment with a single space:
> >> >
> >> > #include<stdio. h>
> >> > int main(void){i\
> >> > nt c,p=-1,k=0,s=0
> >> > ;while((c=getch ar
> >> > ())!=EOF){if(s= =0
> >> > ){if(p=='/'&&c==
> >> > '*'){s=1;k=2;}e l\
> >> > se if(c=='\"'&&p
> >> > !='\\'&&p!='\'' )s
> >> >=2;}else if(s==1)
> >> > {if (p=='*'&&c==
> >> > '/')s=0;}else if(
> >> > s==2){if(c=='\" '
> >> > &&p!='\\')s=0;} if
> >> > (k==1)putchar(' '
> >> > );if(p>0&&s!=1) {
> >> > if(!k)putchar(p );
> >> > if(--k<0)k=0;}p=c
> >> > ;}putchar(p);re t\
> >> > urn 0;}
> >>
> >> It doesn't handle line-splicing. Also, putchar(-1) is not portable.[/color]
> >
> > putchar(-1) is never executed in the above code[/color][/color]
.... unless one tries to remove the comments from an empty file. :-)
[color=blue]
> putchar(-1) is executed if EOF is encountered immediately.[/color]
So just insert 'if (p > 0)' before the final putchar.
--
Tim Hagan
Comment