0

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.

Gathos
  • 3
  • 1
  • 1
    Please provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). There are errors in your code that could result in the symptoms described but it is not clear whether that is your real code. For example, what is `i`? Also you are printing the `distance` twice instead of `end`. – kaylum Dec 06 '19 at 01:38
  • Is is very hard to help you if you don't show more of the code. – BobRun Dec 06 '19 at 01:39
  • 1
    `data* vertexes = (data*)malloc(sizeof(data))` suggests you allocate only a single `struct data;` – David C. Rankin Dec 06 '19 at 01:55
  • Was `i` set to `0` when the `fscanf()` was called? Otherwise, you're printing different data from what you read. (And, since you only allocated one row of data, `i` must be `0` or you write out of bounds.) – Jonathan Leffler Dec 06 '19 at 02:33

1 Answers1

1

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);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Themathix
  • 56
  • 6
  • Yeah, it was amisstype from my side. Its separated witht tabs so its okay. I has some index issue within my program part. I should program at day and not midnigth. thanks in advance. – Gathos Dec 06 '19 at 02:49
  • White space (blanks, tabs, newlines, etc) in `fscanf()` format strings indicate places where optional white space can appear. [It is a UI disaster if white space appears at the end of a format string](https://stackoverflow.com/questions/19499060/what-is-difference-between-scanfd-and-scanfd). Because the numeric formats skip leading white space anyway, the spaces between the conversion specifications are not necessary (so `"%d%d%lf"` would work fine). The tabs in the original are harmless. On output, the story with tabs is more complicated — by default, they represent 1-8 blanks (not 1-4). – Jonathan Leffler Dec 06 '19 at 03:20
  • 1
    Good spotting the typo — I missed that. – Jonathan Leffler Dec 06 '19 at 03:21