简体   繁体   中英

Fscanf can't scan the values from the row properly

So I want to read the lines and save them dinamcly in a array the lines. Can you help me foind the problem, because I can't scan the last value (distance) from the line.

Code:

[...]
typedef struct {
    int start;
    int end;
    double distance;
} data;
[...]
data* vertexes = (data*)malloc(sizeof(data))
FILE* f= fopen("option_c.txt", "r");

if (f == NULL)
{
    printf("\n\nThe program couldn't read in the 'option_a.txt' file. The program is going to stop");
        return NULL;
}

fscanf(f,"%d\t%d\t%lf", &vertexes[i].start, &vertexes[i].end, &vertexes[i].distance);
printf("%d\t%d\t%lf", vertexes[0].start, vertexes[0].distance, vertexes[0].distance);
[...]

my files first row is 1 0 1 my output is: 1 0 0

I go throw my file and I can't scan the last value, but I don't know where to search the source of the problem. My input is clear.

Thanks in advance.

You do realize you are printing distance twice, right?

Another thing, \\t is a escape character for 'tab', so I believe it's default 4 spaces, so if you have only one space character separating your values on the file, you better use:

fscanf(f,"%d %d %lf", &vertexes[i].start, &vertexes[i].end, &vertexes[i].distance);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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