首页 > 解决方案 > 删除评论的程序,只有一个文件

问题描述

while((current = fgetc(file_with_comments)) != EOF){
  exit_flag = false;
  switch(current){
     case '/':
        if((current = fgetc(file_with_comments)) == '*'){
           do{
              if((current = fgetc(file_with_comments)) == '*'){
                 if((current = fgetc(file_with_comments)) == '/'){
                    exit_flag = true;
                 }
              }
           }while(exit_flag == false);
        }
        if(exit_flag == false){
           array_of_char[index] = '/';
           index++;
        }
        break;

     case '*':
        printf("does u go in here\n");
        break;

     default:
        array_of_char[index] = current;
        index++;
  }
}

所以我正在尝试构建这个从 .c 文件中删除注释的程序(不是单行注释所以 // )

我尝试了很多不同的方法,这是我最新的也是迄今为止最成功的方法。但是,如果它应该起作用,它不适用于某些事情。

例如,如果我将其用于输入

/**** This is a comment with a bunch of stars in it ****/
//* This is a comment immediately after a slash */
/* This is a comment immediately before a slash *//

除了最后一个'/',输出将是正确的,程序将删除它,我不知道为什么。感谢您的帮助!

编辑:刚刚意识到我在代码中留下了'*'的情况,这可以忽略,因为我没有使用它。

标签: ccomments

解决方案


推荐阅读