Reputation: 3
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.
Upvotes: 0
Views: 67
Reputation: 56
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);
Upvotes: 1