【问题标题】:C fgetc sigsegv - skipping to end of lineC fgetc sigsegv - 跳到行尾
【发布时间】:2014-12-03 05:41:11
【问题描述】:

我正在逐个字符地读取文件。当我到达冒号时,我想跳过所有字符,直到到达换行符。实际上,在看到冒号后,我希望跳到下一行(如果存在另一行)。 看起来很简单,但是当我尝试跳出第二个 while 循环时,我收到了一个 sigsegv,它跳过了我不关心的数据。

没有中断,代码的行为就像我期望的那样(尽管不是我想要的输出)。也就是说,它读取数据直到第一个冒号,然后它会跳到EOF并退出。

      5    FILE *fp = fopen("myFile", "r");
      6    char *string = (char*) malloc(sizeof(char));
      7    char *head = string;
      8  if( fp ){
      9       int c;
     10       while( (c=fgetc(fp)) != EOF ){
     11          if(c == ':'){
     12             *string++ = '\n';
     13             while( (c=fgetc(fp)) != EOF ){
     14                if( c == '\n' )             // Skip to end of line
     15                   break; //removing break avoids sigsegv
     16             }
     17          }else{
     18             *string++ = c;
     19          }
     20       }
     21    }

似乎当我跳出循环时,cfp 以某种方式被修改,导致 sigsegv。我最好的猜测是fp 以某种方式被修改并在父fgetc() 调用fp 时生成此错误。不过,除此之外,我不确定是什么导致了这个问题。

【问题讨论】:

标签: c segmentation-fault fgetc


【解决方案1】:

您需要为string 分配更多字节。这一行:

char *string = (char*) malloc(sizeof(char));

只为你的字符串分配一个字节。

【讨论】:

  • 你能解释一下为什么如果我删除中断不会给出错误吗?即使我不分配更多内存,它也能打印出许多字符。老实说,这对我来说似乎很奇怪。
【解决方案2】:
the main problem was writing to offset from a pointer
that did not point anywhere in particular
which is a major reason for getting seg fault events
note: 'string' is a well known C++ class name
      so changed the name to pBuffer
The following code snippet fixes that problem (and a few others)


    int   memorySize = 1024;
    char *realloc_result = NULL;
    char *pBuffer = malloc(memorySize);
    if( NULL == pBuffer )
    { // then, malloc failed
        perror( "malloc failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    FILE *fp = fopen("myFile", "r");
    if( NULL == fp )
    { // then fopen failed
        perror( "fopen failed for read" );
        fclose(fp);
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful


    int c;     // input buffer char
    int i = 0; // loop counter
    while( EOF != (c=fgetc(fp)) )
    {
        if( i >= memorySize )
        { // then need more memory

            if( NULL == (realloc_result = realloc( pBuffer, memorySize*2 ) )
            { // then realloc failed

                perror( "realloc failed" );
                free(pBuffer);
                fclose(fp);
                exit( EXIT_FAILURE );
            }

            // implied else, realloc successful

            memorySize *= 2; // track current amount of allocated memory
            pBuffer = realloc_result; // update ptr to allocated memory
        } // end if

        if( ':' == c )
        { // then, terminate buffered line and skip to end of input line

            pBuffer[i] = '\n';
            i++;

            // skip to end of line
            while( EOF != (c=fgetc(fp)) )
            {
                if( '\n' == c )
                { // then, reached end of input line
                    break; // exit inner while
                } // endif
            } // end while

            if( EOF == c )
            { // then, at end of file
                break; // exit outer while
            } // end if
        }

        else
        { // not skipping data
            pBuffer[i] = c;
            i++;
        } // end if
    } // end while

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2015-05-28
    相关资源
    最近更新 更多