修改后:删除C源文件注释
感谢 ICU 提醒我昨天的程序有严重BUG。修改之后,用ICU的例子做了测试,完美删除注释,测试的比较少,不排除依旧存在BUG。
程序代码:
/*删除C源代码中的注释 PS:注释不允许嵌套*/ #include <stdio.h> #include <stdlib.h> #define MAXLINE 100000 int getline(char s[], int size, FILE * fp); void del_annotation(char s[]); int main(void) { char line[MAXLINE]; int i; FILE *p_file_r, *p_file_w; if((p_file_r = fopen("words.txt","r")) == NULL)//测试文件 { fprintf(stderr,"无法打开读取文件"); exit(EXIT_FAILURE); } if((p_file_w = fopen("word1.txt","w")) == NULL)//测试文件 { fprintf(stderr,"无法打开写入文件"); exit(EXIT_FAILURE); } while((i = getline(line,MAXLINE,p_file_r)) > 0) { del_annotation(line); fprintf(p_file_w,"%s",line);//将跳过所有注释的字符串写入新的文本。 } fclose(p_file_r); fclose(p_file_w); printf("Bye!\n"); return 0; } int getline(char s[], int size,FILE * fp)// { int i,c; for(i = 0; i < size - 1 && (c = fgetc(fp)) != EOF; i++) s[i] = c; s[i] = '\0'; return i; } void del_annotation(char s[])//删除C源文件中的注释; { int i,j; for(i = 0, j = 0; s[i] != '\0'; i++) { if(s[i] == '/') { if(s[i+1] == '*') { i += 2; while(s[i] != '*' || s[i + 1] != '/') i++; i += 2; if(s[i] == '/') { i--; continue; } } else if(s[i+1] == '/') while(s[i] != '\n') i++; } else if(s[i] == '\"' || s[i] == '\'') { char x = s[i]; while(s[j++] = s[i++] , s[i] != x && s[i] != '\0') if(s[i] == '\\') s[j++] = s[i++]; } s[j++] = s[i]; } s[j] = '\0'; }
程序代码:
//测试用例1: int main(int argc, char* argv[]) { /* ///*/int a=1;/* */ printf("\"/*a=%d",a);printf("\"*/");return 0; return 0; }
程序代码:
//删除之后 int main(int argc, char* argv[]) { int a=1; printf("\"/*a=%d",a);printf("\"*/");return 0; return 0; }
程序代码:
//测试用例2: int main(int argc, char* argv[]) { /* ///*/int a=1;/* */ char ch[]="\ /*/*abcdef\ ghijk*/"; printf("\"/*a=%d\n",a);printf("\"ch=%s*/",ch);return 0; return 0; }
程序代码:
//删除之后 int main(int argc, char* argv[]) { int a=1; char ch[]="\ /*/*abcdef\ ghijk*/"; printf("\"/*a=%d\n",a);printf("\"ch=%s*/",ch);return 0; return 0; }
程序代码:
//测试用例3: int main(void) { char line[MAXLINE]; int i; FILE *p_file_r, *p_file_w;/*fsaghgdagsdhkfhsadiglhsaildghsaglikhsa*/ /*fsadhgsi aoghsai guyhsaighs aigsaiug yhwe8iu4gyhs eiegvsdk jghbnksla*/ if((p_file_r = fopen("words.txt","r")) == NULL)//测试文件 /*gfsdgusaoighujsaiog gjsdiaghsaighsai gjsdkaghsadikgl ghskadghsakgh*/*/ { fprintf(stderr,"无法打开读取文件"); exit(EXIT_FAILURE); } if((p_file_w = fopen("word1.txt","w")) == NULL)//测试文件 { fprintf(stderr,"无法打开写入文件"); exit(EXIT_FAILURE); } while((i = getline(line,MAXLINE,p_file_r)) > 0) { del_annotation(line); fprintf(p_file_w,"%s",line);//将跳过所有注释的字符串写入新的文本。 } fclose(p_file_r); fclose(p_file_w); printf("Bye!\n"); return 0; }
程序代码:
//删除之后 int main(void) { char line[MAXLINE]; int i; FILE *p_file_r, *p_file_w; if((p_file_r = fopen("words.txt","r")) == NULL) { fprintf(stderr,"无法打开读取文件"); exit(EXIT_FAILURE); } if((p_file_w = fopen("word1.txt","w")) == NULL) { fprintf(stderr,"无法打开写入文件"); exit(EXIT_FAILURE); } while((i = getline(line,MAXLINE,p_file_r)) > 0) { del_annotation(line); fprintf(p_file_w,"%s",line); } fclose(p_file_r); fclose(p_file_w); printf("Bye!\n"); return 0; }
[此贴子已经被作者于2017-2-10 08:01编辑过]