在C中删除内联注释时出现意外行为

问题描述 投票:1回答:2

堆栈溢出!我正在学习使用C技术的学习过程。我有一个函数,它获取一个输入文件,搜索文件并将内容写入输出文件而不发表评论。该功能有效,但在某些情况下也会制动。我的职责:

    void removeComments(char* input, char* output)
{
    FILE* in = fopen(input,"r");
    FILE* out = fopen(ouput,"w");
    char c;
    while((c = fgetc(in)) != EOF)
    {
       if(c == '/')
       {

          c = fgetc(in);
          if(c == '/')
          {
           while((c = fgetc(in)) != '\n');
          }
          else
          {
            fputc('/', out);
          }
       }
       else
       {
           fputc(c,out);
       }
    }
    fclose(in);
    fclose(out);
}

但是当我将这样的文件作为输入时:

// Parameters: a, the first integer; b the second integer.
// Returns: the sum.
int add(int a, int b) 
{
    return a + b; // An inline comment.
}
int sample = sample;

删除内联注释时,由于某种原因它无法到达'\ n'并且它提供输出:

int add(int a, int b) 
{
    return a + b; }
int sample = sample;

[编辑]感谢您的帮助!它适用于我发布的情况,但它在另一个中制动。当前代码:

FILE* in = fopen(input,"r");
FILE* out = fopen(output,"w");

if (in == NULL) {
  printf("cannot read %s\n", input);
  return; /* change signature to return 0 ? */
}
if (out == NULL) {
  printf("cannot write in %s\n", output);
  return; /* change signature to return 0 ? */
}

int c;
int startline = 1;

while((c = fgetc(in)) != EOF)
{
   if(c == '/')
   {
      c = fgetc(in);

      if(c == '/')
      {
        while((c = fgetc(in)) != '\n')
        {
          if (c == EOF) {
            fclose(in);
            fclose(out);
            return; /* change signature to return 1 ? */
          }
        }
        if (! startline)
          fputc('\n', out);
        startline = 1;
      }
      else if (c == EOF)
        break;
      else {
        fputc('/', out);
        startline = 0;
      }
   }
   else
   {
     fputc(c,out);
     startline = (c == '\n');
   }
}

fclose(in);
fclose(out);

当文件包含除法时,第二个变量消失。例:

int divide(int a, int b) 
    {
        return a/b; 
    }

它回馈:

int divide(int a, int b) 
    {
        return a/; 
    }
c comments c-preprocessor
2个回答
3
投票

while((c = fgetc(in)) != '\n');

你需要一个fputc('\n', out);

补充说明:

char c;
while((c = fgetc(in)) != EOF)

c必须是一个int才能管理EOF

只是一个错字:输出必须输出才能编译

阅读“/”后,您无法很好地管理EOF

你错过了检查fopen的结果


一份提案 :

#include <stdio.h>

void removeComments(char* input, char* output)
{
    FILE* in = fopen(input,"r");
    FILE* out = fopen(output,"w");

    if (in == NULL) {
      printf("cannot read %s\n", input);
      return; /* change signature to return 0 ? */
    }
    if (out == NULL) {
      printf("cannot write in %s\n", output);
      return; /* change signature to return 0 ? */
    }

    int c;

    while((c = fgetc(in)) != EOF)
    {
       if(c == '/')
       {
          c = fgetc(in);

          if(c == '/')
          {
            while((c = fgetc(in)) != '\n')
            {
              if (c == EOF) {
                fclose(in);
                fclose(out);
                return; /* change signature to return 1 ? */
              }
            }
            fputc('\n', out);
          }
          else if (c == EOF) {
            fputc('/', out);
            break;
          }
          else
            fputc('/', out);
            fputc(c, out);
       }
       else
       {
         fputc(c,out);
       }
    }

    fclose(in);
    fclose(out);
     /* change signature to return 1 ? */
}

int main(int argc, char ** argv)
{
  removeComments(argv[1], argv[2]);
}

正如Tormund Giantsbane在评论中所说的那样,最好完全删除仅包含注释的行(从第一列开始的注释),新提案会这样做:

#include <stdio.h>

void removeComments(char* input, char* output)
{
    FILE* in = fopen(input,"r");
    FILE* out = fopen(output,"w");

    if (in == NULL) {
      printf("cannot read %s\n", input);
      return; /* change signature to return 0 ? */
    }
    if (out == NULL) {
      printf("cannot write in %s\n", output);
      return; /* change signature to return 0 ? */
    }

    int c;
    int startline = 1;

    while((c = fgetc(in)) != EOF)
    {
       if(c == '/')
       {
          c = fgetc(in);

          if(c == '/')
          {
            while((c = fgetc(in)) != '\n')
            {
              if (c == EOF) {
                fclose(in);
                fclose(out);
                return; /* change signature to return 1 ? */
              }
            }
            if (! startline)
              fputc('\n', out);
            startline = 1;
          }
          else if (c == EOF) {
            fputc('/', out);
            break;
          }
          else {
            fputc('/', out);
            fputc(c, out);
            startline = 0;
          }
       }
       else
       {
         fputc(c,out);
         startline = (c == '\n');
       }
    }

    fclose(in);
    fclose(out);
     /* change signature to return 1 ? */
}

int main(int argc, char ** argv)
{
  removeComments(argv[1], argv[2]);
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -g r.c
pi@raspberrypi:/tmp $ cat i
// Parameters: a, the first integer; b the second integer.
// Returns: the sum.
int add(int a, int b) 
{
    return a + b/c; // An inline comment.
}
int sample = sample;
pi@raspberrypi:/tmp $ ./a.out i o
pi@raspberrypi:/tmp $ cat o
int add(int a, int b) 
{
    return a + b/c; 
}
int sample = sample;

正如DavidC所说。在一条评论中如果//放在一个字符串中,结果将不是预期的结果,在字符中也是如此,即使是非法的(我的意思是'//'也不能改变),C评论怎么样(/ * .. // ... * /)等


2
投票

删除内联注释时,由于某种原因无法访问'\ n'

好吧,如果它无法在内联评论结束时到达或看到换行符,那么程序可能会消耗整个文件的其余部分。它实际上没有做的是将这些换行符写入输出。

考虑一下您的评论吃代码:

           while((c = fgetc(in)) != '\n');

当读取换行符时,该循环终止。此时,已经读取的换行符无法再次从输入中读取,因此您的一般读/写规定将无法处理。如果您希望保留这些换行符,则需要在注释处理分支中打印它们。

补充说明:

  1. fgetc返回int,而不是char,你需要处理它,以便能够正确检测文件结束。
  2. 如果输入以未通过换行符终止的内联注释结束,则程序将进入无限循环。这种来源在技术上是不符合要求的,但即便如此,你也应该处理它。
© www.soinside.com 2019 - 2024. All rights reserved.