/* * This program removes comments from the program named on the command line. */ #include int main (int argc, char *argv[]) { char s[1000]; int i, commenting; FILE *f; /* open the file named on the command line */ if (argc != 2) { fprintf (stderr, "Usage: %s \n", argv[0]); exit (1); } f = fopen (argv[1], "r"); if (!f) { perror (argv[1]); exit (1); } /* currently, "commenting" is false, i.e., we're not in a comment */ commenting = 0; for (;;) { /* get a string from the file */ fgets (s, 1000, f); /* if we're at the end of file, get out of the loop */ if (feof (f)) break; /* for each character in the string... */ for (i=0; s[i]; i++) { /* if we aren't yet commenting and see "/*", * then begin we know we're starting a comment */ if (!commenting && s[i] == '/' && s[i+1] == '*') commenting = 1; /* if we're not commenting, print the current char */ if (!commenting) putchar (s[i]); /* if we are currently commenting and see "(you know)", * end commenting and increment i past the / */ if (commenting && s[i] == '*' && s[i+1] == '/') { commenting = 0; i++; } } } fclose (f); /* if we're still in a comment, warn the user */ if (commenting) fprintf (stderr, "unfinished comment!\n"); exit (0); }