Here's a utility I wrote at some point. You might find it useful:
Code:
itsme@dreams:~/C$ cat comments.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
int reverse = 0;
int main(int argc, char **argv)
{
char *filename;
FILE *fp;
char buf[4096], buf2[4096], *p;
int incomment = 0;
if(argc == 3)
{
if(!strcmp(argv[1], "-r"))
reverse = 1;
filename = argv[2];
}
else if(argc == 2)
filename = argv[1];
else
{
puts("Usage: comments [-r] <filename>");
return 1;
}
if(!(fp = fopen(filename, "r")))
{
perror("fopen()");
return 1;
}
while(fgets(buf, sizeof(buf), fp))
{
if(!incomment && (p = strstr(buf, "//")))
{
if(reverse)
{
strncpy(buf2, buf, p-buf);
buf2[p-buf] = '\0';
puts(buf2);
}
else
fputs(p, stdout);
continue;
}
for(p = buf;*p;p++)
{
if(incomment)
{
if(*p == '*' && *(p+1) == '/')
{
p++;
incomment = 0;
if(!reverse)
puts("*/");
}
else
{
if(!reverse)
putchar(*p);
}
}
else
{
if(*p == '/' && *(p+1) == '*')
{
p++;
incomment = 1;
if(!reverse)
fputs("/*", stdout);
}
else
{
if(reverse)
putchar(*p);
}
}
}
}
fclose(fp);
return 0;
}
If used on a test file like this:
Code:
#include <stdio.h>
// A hello world program
int main(void)
{
/* print it */ printf("Hello, world!\n"); /* Done */
return 0; // return to OS
}
It produces output like this:
Code:
itsme@dreams:~/C$ ./comments test.c
// A hello world program
/* print it */
/* Done */
// return to OS
itsme@dreams:~/C$
Or like this in reverse:
Code:
itsme@dreams:~/C$ ./comments -r test.c
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
itsme@dreams:~/C$
You, personally, might find it useful by running it like: comments -r file.c > filenocomments.c