0

I have text file with multiple lines. Like:

11111111
22222222
33333333
44444444
55555555
...

I wrote a c code to retrieve each line. My code parsed all lines and wrote them to output console succesfully. But after the last line app crashed. It returns

Program received signal SIGSEGV, Segmentation fault. 

Why is that?

My C Code:

FILE *fPtr;
char file[]="/root/dd";

char *rest;
char *token;
char *buffer;

unsigned long size;

fPtr = fopen(file,"r"); 

fseek(fPtr, 0, SEEK_END);
size=(unsigned long)ftell(fPtr);
fseek(fPtr, 0, SEEK_SET);

buffer=(char *)malloc(size);    

if(fPtr)
{   
    while(fgets(buffer, size, fPtr))    
    {
        while(token = strtok_r(buffer, "\n", &rest))
        {
            printf("token: %s\n", token);
            buffer = rest;
        }
    }
    fclose(fPtr);
}
else
{
     printf("file not open \n");
}

UPDATE

I thins problem is not related with strtok_r(). Because I changed my code:

FILE *fPtr;
char file[]="/root/dd";

char *rest;
char *token;
char *buffer;

unsigned long size;

fPtr = fopen(file,"r");

if(fPtr==NULL)
{
     printf("null pointer\n");
}

fseek(fPtr, 0, SEEK_END);
size=(unsigned long)ftell(fPtr);
fseek(fPtr, 0, SEEK_SET);

buffer=(char *)malloc(size);    

if(fPtr)
{   
    while(fgets(buffer, size, fPtr))    
    {
         printf("buffer: %s\n", buffer);
    }
    fclose(fPtr);
}
else
{
     printf("file not open \n");
}

And still same thing happens.

utarid
  • 1,642
  • 4
  • 24
  • 38
  • 1
    You should check `fPtr` for null *before* you try to use it, instead of after the fseeking. –  Oct 08 '13 at 14:30
  • [Don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). Also, you allocate room for the entire file, then still use `fgets()` to read it line by line. That's two halves of two *different* solutions to the problem of loading a file ... – unwind Oct 08 '13 at 14:39
  • Is `size` the value you expect? You should also check `buffer` for null before you use it. – Steve Oct 08 '13 at 15:02

3 Answers3

2

I think you call to strtok_r is wrong, quote from the manual

#include

char *strtok_r(char *s1, const char *s2, char **s3);

To get the first token from s1, strtok_r() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter.

CS Pei
  • 10,869
  • 1
  • 27
  • 46
0

The second and subsequent calls to strtok_r should have the first parameter as NULL. However, I'm not sure this is why you have the segfault.

You are also changing where buffer points to in the line

buffer = rest;

when you have read the whole file and exit the fgets line runs again, buffer no longer points to a block of memory of size size. I suspect this is causing your segfault.

Also, by modifying buffer you have no way of freeing the memory that was malloc

Steve
  • 7,171
  • 2
  • 30
  • 52
0

problem is definition of char c[]="...";

it should be char c[20]="...";

utarid
  • 1,642
  • 4
  • 24
  • 38