0

In the code, when I print var_value, it shows its content but when I need to assign it later on the if else statements, it's empty only IN THE LAST IF and I have no idea why it is. If I delete the last statement, the other three pass without problems.

    while ((read = getline(&line, &len, f)) != -1){
    printf("%s\n", line);
    char *token;
    token = strtok(line, "=");
    var_name = token;

    /* Separate every line by the '=' character */
    while( token != NULL ) {
            var_value = token;    
            token = strtok(NULL, "=");
    }
    printf("%s\n", var_name);
    printf("%s\n", var_value);

    // Obtain the parameters
    if (strcmp(var_name, "puerto") == 0) {
        puerto = atoi(var_value);
        parameters_count += 1;
    } else if (strcmp(var_name, "tamano_tabla") == 0) {
        tamano_tabla = atoi(var_value);
        parameters_count += 1;
    } else if (strcmp(var_name, "periodo_archivo") == 0) {
        periodo_archivo = atoi(var_value);
        parameters_count += 1;
    } else if (strcmp(var_name, "archivo_tabla") == 0) {
        printf("%s var val\n", var_value);
        strcpy(archivo_tabla, strtok(var_value, "\n")); //Remove \n and copy to destination variable
        parameters_count += 1;
        printf("%s filetabla\n", archivo_tabla);
    }
}

Edit: Results in console and after the final one, segmentation fault

puerto=1212

puerto
1212

archivo_tabla=tabla.xml

archivo_tabla
tabla.xml

tabla.xml
 var val
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 3
    Are you aware that [`strtok`](https://linux.die.net/man/3/strtok) modifies its first parameter? – Mathieu Feb 05 '18 at 21:08
  • 3
    There's not enough here to tell what the problem is. Please update your question with a [mcve]. – dbush Feb 05 '18 at 21:11
  • We have some guesses about what the problem might be, but you have not given us enough information to give you a confident answer. Present an [mcve], and do not forget to include in it some input that produces the unwanted behavior. – John Bollinger Feb 05 '18 at 21:12
  • 1
    Added the complete while code – Madelyne Velasco Mite Feb 05 '18 at 21:16
  • @purplepsycho: And worse, it uses global state, so attempts to switch back and forth between strings being tokenized (even in completely different functions) won't work. `strtok_r` (`strtok_s` on Windows) at least avoids this problem. – ShadowRanger Feb 05 '18 at 21:24
  • @MadelyneVelascoMite: This code still isn't complete. Where does the `archivo_tabla` variable come from? Is it stack allocated? `malloc`-ed? Global? The most likely cause of this problem would be if it was a `NULL` pointer, or (in some cases) a buffer smaller than the string being copied into it. – ShadowRanger Feb 05 '18 at 21:30

2 Answers2

1

Your output does not support your contention that

In the code, when I print var_value, it shows its content but when I need to assign it later on the if else statements, it's empty only IN THE LAST IF [...].

In fact, your output shows the opposite: var_val is printed just fine. I have to assume that you are being confused by the fact that its value ends with a newline, which is printed, too. Thus, " var val" appears at the beginning of a line. Here's the expected value of var_val, including newline, followed by " var val":

tabla.xml
 var val

The presence of a newline in the string provided by getline() is the whole point of the strtok(var_value, "\n") that happens next, after all. Or so I assume.

Note also that although the output you present appears to be truncated relative to the code you present, in my tests, the contents of var_val are successfully copied to variable archivo_tabla, too, less that pesky newline.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
-1

This line is suspect: strcpy(archivo_tabla, strtok(var_value, "\n")); //Remove \n and copy to destination variable

strtok mutates var_value. You seem to be copying archivo_tabla to the remaining part of var_value after the "\n" (which doesn't really make sense)

https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

information_interchange
  • 2,538
  • 6
  • 31
  • 49
  • Yes, but that wouldn't affect the `printf()` that appears prior to that line. – John Bollinger Feb 05 '18 at 21:10
  • That's true; perhaps it is being run in a loop? We don't have the full picture of the control flow – information_interchange Feb 05 '18 at 21:12
  • It's inside a loop. The printf outside the if prints what var_value does but before the strtok there is another printf and it returns nothing. – Madelyne Velasco Mite Feb 05 '18 at 21:15
  • 1
    @information_interchange: Umm, you've got the `strcpy` args backwards. The destination is the first argument, so it's copying *to* `archivo_tabla`, not *from* it. And the first `strtok` call returns the address of first non-empty data before a `\n`, not after; it's effectively copying whatever is there after stripping leading newlines, through to first newline after the first non-newline character. – ShadowRanger Feb 05 '18 at 21:26