sscanf mess up variables only under Linux

Hi everybody,

recently I've decided to update a c++ programs that I've written for Windows in order to let it run on Linux.
This program is a console application that calculates some geometrical properties of a section and it takes input data from a text file.

The program checks if the two commands general data (named GENR) and END have been written in the text file. If so the booleans gda (for GENR) and endfile (for END) are going to be set true.

Under Windows the program runs fine...no errors...everything as expected.
When I debug the code under Linux I get a weird behavior:
The program reads the first line of the input file (in this case GENR) and I don't know why when it gets to the sscanf it overwrites both booleans gda and endfile with the integers 69 and 78. In this way the if(!endfile) throws me into the "Error: cannot find the command END".

Below I post only an extract of the code, which does basically nothing useful but it shows this behavior on my Linux machines.

Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>

int main()
{
    int n, N_mat, N_pnt, N_soa, N_hoa, N_bar;
    bool gda = false;
    bool endfile = false;
    char txt[128];
    char rts;

    // Read input file
    FILE *file = fopen("Testfile.txt", "rt");

    do {
        // Read line
        fgets(txt, 128, file);

        // If general data command is found
        if( txt[0]=='G' && txt[1]=='E' && txt[2]=='N' && txt[3]=='R' )
        {   n = sscanf(txt, "%s %d %d %d %d %d\n", &rts, &N_mat, &N_pnt, &N_soa, &N_hoa, &N_bar);
            gda = true;
        }

        // If end command is found
        if( txt[0]=='E' && txt[1]=='N' && txt[2]=='D' )
            endfile = true;
    }
    while( !gda || !endfile );

    // Missing commands
    if( !gda )
        printf("    !!! Error: cannot find the command GENR\n");
    if( !endfile )
        printf("    !!! Error: cannot find the command END\n");

    fclose(file);

    return 0;
}


And the input file Testfile.txt
1
2
3
GENR 1 8 1 0 0

END
Last edited on
n = sscanf(txt, "%s %d %d %d %d %d\n", &rts, &N_mat, &N_pnt, &N_soa, &N_hoa, &N_bar)
rts is a single char but you a string into it. -> %s
I guess it's undefined behavior so it works on Windows but not on Linux.
> n = sscanf(txt, "%s %d %d %d %d %d\n", &rts, &N_mat, &N_pnt, &N_soa, &N_hoa, &N_bar);
Well lying about the size of your buffer won't help.
> char rts;
You've already established that it's 4 characters.

Other things.
1. Linux doesn't care about "t" in your open mode.
2. You should do dos2unix ( https://en.wikipedia.org/wiki/Unix2dos ) on your text files.
3. Appending a \n to your sscanf string doesn't do anything, except possibly barf on the \r at the end of your DOS format file.

Thomas1965 and salem c,

thank you both for your reply and the other tips.
This was the solution...i was lying about the size of rts!
Topic archived. No new replies allowed.