C fgetc sigsegv - 跳到行尾

时间:2014-12-03 05:41:12

标签: c sigsegv fgetc

我正在通过char读取文件char。当我到达冒号时,我想跳过所有角色,直到我到达换行符。实际上,在看到冒号后我想跳到下一行(如果存在另一条线)。 看起来很简单,但是当我试图突破我的第二个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时生成此错误。除此之外,我不确定是什么导致了这个问题。

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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