#Segmentation Fault

1 messages · Page 1 of 1 (latest)

winged pendant
#

I have a program that reads a text file and uses fgets() to read line by line. I also used strtok_r() to tokenise each word that is seperated by a space. It works for the first line but then returns segmentation fault after.
Text file:

# execute a command at 3AM every day
0 3 * * *      daily-backup
#
# execute a command at 4:15AM every Sunday
15 4 * * sun   weekly-backup
#
# start thinking about the project....
0 10 22 7 mon  deep-thought
#
# submit my project automatically, just in case I forget!
59 16 16 8 *   submit-project
#
# mail out a monthly newsletter
0 2 1 * *      send-monthly-newsletter
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char *filename = "crontab-file.txt";

    FILE *pFile = fopen(filename, "r");

    if (!pFile)
    {
        printf("error loading file '%s'\n", filename);
        return 400;
    }

    fseek(pFile, 0, SEEK_END);
    int size = ftell(pFile);
    rewind(pFile);

    char *buffer = (char *)malloc(sizeof(char) * (size + 1));
    char *token;

    while (fgets(buffer, (size + 1), pFile))
    {
        if (buffer[0] == '#')
            continue;
        while ((token = strtok_r(buffer, " ", &buffer)))
        {
            printf("Token: %s\n", token);
        }
    }

    fclose(pFile);
    return 0;
}
winged pendant
#

stil need help pls

fossil wharf
#

maybe it has something to do with strtok_r maintains a state with buffer

#

then its changed with the next fgets

winged pendant
#

Should i use sscanf instead?

fossil wharf
#

why not just strtok?

winged pendant
#

I heard its like less stabile

#

Or more dangerous

void orchid
#

its not thread safe

#

but you arent using threads so its fine to use

winged pendant
#

Oh okay

#

Could u elaborate on that

#

Im quite new to c and came from python

fossil wharf
#

it looks like they both have some internal state, strtok has a global state so it isnt thread safe, strtokr you are passing it as an argument

void orchid
#

yeah that

winged pendant
#

Ohhh

#

Yeah i think i get it

void orchid
#

anyway youre using strtok_r() wrong, youre only supposed to pass buffer on the first call, and the 3rd arg shouldnt point to buffer, it should be another pointer that strtok_r() can store its state in

winged pendant
#

Do i just create another variable

void orchid
#

yeah

void orchid
winged pendant
#

So strtok_r(NULL, “ “, buffer)?

void orchid
#

no

#

on the first use of strtok_r(), call it as such: strtok_r(buffer," ",&state). on subsequent uses, call it as such: strtok_r(NULL," ",&state)

winged pendant
#

&state?

void orchid
#

a separate char* variable to store the state

winged pendant
#

Ok right

void orchid
#

cant use buffer or you cant free it later

winged pendant
#

I’ll try it

#

Thanks

void orchid
#

np

north lindenBOT
#

@winged pendant

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

#

@winged pendant

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.