0
FILE *fp;

fp = fopen(filen, "wb");

const char tok[2] = ",";
char str[340];

while (fgets(str, 340, stdin) != NULL)
{
  struct test loadTest;
  printf("You entered: %s", str);
  strncpy(loadTest.level, strtok(str, tok), 20);
  strncpy(loadTest.first, strtok(NULL, tok), 30);
  fwrite(&loadTest, sizeof(struct test), 1, fp);
}

fclose(fp);

Hello all,

For some reason I'm getting a segmentation fault error in my code.

I'm almost positive the error is somewhere within the small code block above (since that's all I modified for the seg fault), but I can't seem to pinpoint it.

I know segmentation faults have to do with accessing memory I shouldn't be accessing, but I'm not sure where I am even doing that in the code.

Any help would be greatly appreciated!

dooder
  • 529
  • 1
  • 6
  • 14
  • 2
    Have you tried to debug the code? – hrust Aug 30 '17 at 05:29
  • 4
    Are you sure that length of tokens is fixed to specified 20 and 30? – Lemonov Aug 30 '17 at 05:35
  • Where do you check that you successfully opened the output file? – Jonathan Leffler Aug 30 '17 at 05:37
  • Why not use `sizeof(str)` instead of `340` in the call to `fgets()`? Why not use `sizeof(loadTest.level)` instead of `20` in the call to `strncpy()`? Similarly for `loadTest.first`? Why no check on whether the write succeeds? – Jonathan Leffler Aug 30 '17 at 05:40
  • @Lemonov, yes I double checked the token lengths. yes I have tried, it just leads me to segfault after first step for some reason. I have just added those but I'm still getting the error. Thanks for your help though! – dooder Aug 30 '17 at 05:48
  • @dooder try `fwrite(&loadTest, sizeof(loadTest), 1, fp);` – jophab Aug 30 '17 at 06:39
  • 2
    Your debugger will tell you where exactly the segfault occurs. Use it. If you don't know how to use your debugger it's probably a good occasion to start learning now. Also read this please: [mcve]. And don't use `strncpy`, the destination string may be not NUL terminated, read [this](https://stackoverflow.com/questions/1453876/why-does-strncpy-not-null-terminate) – Jabberwocky Aug 30 '17 at 06:56
  • 2
    show the whole code, not just a part. what is `filen`? it is initialized? why you not check if the file is open without error? `if (fp == NULL) return;` 99% that you haven't open a file and `fp` is NULL. – vadim_hr Aug 30 '17 at 07:26
  • 1
    Maybe there is no second `,` and `strtok(NULL, tok)` returns NULL. – i486 Aug 30 '17 at 08:02
  • 2
    Please provide a [mcve]. – n. m. could be an AI Aug 30 '17 at 08:23
  • See whether output file is created and there is something inside or it is empty. – i486 Aug 30 '17 at 08:26

1 Answers1

-1

Some improvements of your code

  • check result of fopen before read/write to file
  • initialize variables before using their values
  • use sizeof instead of constant (as mentioned in comments)
  • strtok() can return NULL and this must be checked (see here why)
  • you must use strncpy() carefully because of this

Here is corrected version of your code

FILE *fp;

fp = fopen(filen, "wb");

if (fp == NULL) 
{
    printf("Error opening file: %s", filen);
}
else
{

    const char tok[2] = ",";
    char str[340];

    while (fgets(str, sizeof(str), stdin) != NULL)
    {
        struct test loadTest;
        char *level;
        char *first;
        memset(&loadTest, 0, sizeof(loadTest)); 
        printf("You entered: %s", str);
        level = strtok(str, tok);
        if (level == NULL)
        {
            continue; // bad input ?
        }
        first  = strtok(NULL, tok);
        if (first == NULL)
        {
            continue;
        }
        strncpy(loadTest.level, level, sizeof(loadTest.level)-sizeof(char));
        strncpy(loadTest.first, first, sizeof(loadTest.first)-sizeof(char));
        fwrite(&loadTest, sizeof(loadTest), 1, fp);
    }

    fclose(fp);
}
vadim_hr
  • 533
  • 5
  • 11
  • These are improvements but don't think they will stop the seg fault. Post them like comment. – i486 Aug 30 '17 at 08:03
  • @i486 what i need to post like comment? I already added comment to the question before posting answer. I think segfault appear because the file isn't opened successfully. `fp` is NULL after `fopen`. – vadim_hr Aug 30 '17 at 08:10
  • You can only suspect that `fopen` is the problem. It is not 100% sure => the solution is for comment, it is not answer. – i486 Aug 30 '17 at 08:24
  • @i486 I can't be 100% sure without see the whole code. only OP can tell why he get segfault error. thanks for your opinion, but i can't delete answer after posting. – vadim_hr Aug 30 '17 at 10:20