前一周做得一道笔试题。 /* * comment_strip.c - strip all the comments in a c source file. * Created : Fri Sep 1 21:38:40 2006 * Zhang Xuecheng <zhangxuecheng1985@gmail.com> * * usage: comment_strip input_file output_file */
/* version: 2.0
changelog: *** fix the bug of "//" , "/*" & "*//" in the former version *** fix the new bug of '"' in the beta version of 2.0 *** optimize the performance by using if...else if... instead of seperate if() *** add makefile and debug info. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
int main (int argc, char *argv[]) { FILE *fp_src, *fp_dst; /* stream for source & destination file */ char c1 = ' ', c2 = ' ';
/* below are flags for comments stripping, if "//" is probed,token1 is set,omit_output is set,output is disabled, if "/*" is probed,token2 is set,omit_output is set,output is disabled. if token1 is set & "/n" is probed,_or_ token2 is set & "*//" is probed, token1 or token2 is cleared,output is enabled, the process of stripping token1&2 should be atomic, at one time,only _one_ token should be stripped. */ int flg_token1 = 0; int flg_token2 = 0; int flg_omit_output = 0; int flg_quote = 0;
if (3 != argc) { fprintf (stderr, "please specify the source & destination file/n"); return 0; }