简体   繁体   English

使用strtok_r()解析文本文件时出现分段错误

[英]Segmentation fault when parsing text file with using strtok_r()

I have text file with multiple lines. 我有多行文本文件。 Like: 喜欢:

11111111
22222222
33333333
44444444
55555555
...

I wrote ac code to retrieve each line. 我写了一个ac代码来检索每一行。 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: 我的C代码:

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(). 我认为问题与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. 仍然发生同样的事情。

I think you call to strtok_r is wrong, quote from the manual 我认为您对strtok_r调用是错误的,请参考手册

#include #包括

char *strtok_r(char *s1, const char *s2, char **s3); 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. 要从s1获得第一个标记,将以s1作为其第一个参数调用strtok_r()。 Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter. s1中剩余的令牌是通过使用第一个参数的空指针调用strtok_r()获得的。

The second and subsequent calls to strtok_r should have the first parameter as NULL . strtok_r的第二次和后续调用应将第一个参数设为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指向行的位置

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 . 阅读完整个文件并退出fgets行后, buffer不再指向大小为size的内存块。 I suspect this is causing your segfault. 我怀疑这是造成您的段错误。

Also, by modifying buffer you have no way of free ing the memory that was malloc 另外,通过修改buffer您无法free malloc

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

it should be char c[20]="..."; 它应该是char c[20]="...";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM