program to remove comments from source
Hi Iv been working on a program that opens a source file then copy's the src to another file excluding all comments. I have the idea and the concept, and have written this src:
Code:
#include <stdio.h>
#define IN 1 /* getc() is now reading comment(not outputting text) */
#define OUT 0 /* getc() isn't reading comment(outputting text) */
int main() {
int mode, l, j, k;
FILE *src, *newSrc;
src = fopen("C:\\1-23.c", "rt");
newSrc = fopen("C:\\src.c", "wt");
mode = OUT;
for (;;) {
/* Entering comment */
if ((l = getc(src)) == EOF) {
printf("l End of file reached");
break;
}
if ((j = getc(src)) == EOF) {
printf("j End of file reached");
break;
}
if (mode == OUT && l == '/' && j == '*') {
mode = IN;
}
else if (mode == OUT && j == '/') {
k = getc(src);
if (k == '*') {
mode = IN;
}
else {
putc(j, newSrc);
putc(l, newSrc);
putc(k, newSrc);
break;
}
}
else if (mode == OUT) {
putc(l, newSrc);
putc(k, newSrc);
}
/* Leaving comment */
if (mode == IN && l == '*' && j == '/') {
mode = OUT;
}
if (mode == IN && j == '/') {
k = getc(src);
if (k == '/') {
mode = OUT;
}
}
}
fclose(src);
fclose(newSrc);
getchar();
return 0;
}
I'm sorry if there's anything hard to understand in the source because i didn't write any comments. When i run this, it creates a blank file with weird characters in it. Can anyone please find were this bug is?