2

I am trying to read values from my text file and store them in struct array. My text file has these values:

names.txt

Num_of_rec: 5
3 7 10 1 red
5 6 8 2 red
9 9 16 5 blue
13 4 19 2 green
12 8 15 4 blue

And my code so far is this:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ERROR -1
#define MAXLEN 256

struct Point {
    float x;
    float y;
};

struct Rectangle {
    struct Point top_left;
    struct Point bottom_right;
    char color[7];
};


int main() {
    int i, N;
    char junk[MAXLEN];
    struct Rectangle *data;
    FILE  *fp;

    fp = fopen("names.txt", "r");

    fscanf(fp,"%s %d\n",junk,&N);
    printf("No: %d", N);

    data = (struct Rectangle *) malloc(N*sizeof(struct Rectangle));

    for(i=0; i<N; i++) {
        fscanf(fp, "%lf %lf %lf %lf %s", data[i].top_left.x, data[i].top_left.y, data[i].bottom_right.x, data[i].bottom_right.y);
    }

    return 0;
}

I want to add all these values in a struct array(data), but I don't know how to do this properly. Until now the output is:

No: 5

and it just crash. I don't understand if the problem is the method that I am using to read the values from the file and store them to the struct array, or something else.

5
  • 3
    For a float type the format specifiers should be %f not %lf so you are probably breaking something, because you only provide 4-byte locations not the 8-byte locations expected. Commented Jan 25, 2019 at 19:21
  • 2
    You're missing data[i].color in the arguments to fscanf(). Commented Jan 25, 2019 at 19:23
  • 2
    Turn on full warnings in your compiler, it should warn you about both of these problems. Commented Jan 25, 2019 at 19:24
  • 3
    You need & before all the other arguments to fscanf(), e.g. &data[i].top_left.x. Commented Jan 25, 2019 at 19:25
  • 2
    Very good effort posting A Minimal, Complete, and Verifiable Example (MCVE). They are so few and far between from new members, it is refreshing to see one. You should validate fp != NULL before using the file pointer and you should also validate every fscanf return before considering the data valid. Commented Jan 25, 2019 at 19:29

1 Answer 1

1
fscanf(fp, "%lf %lf %lf %lf %s", data[i].top_left.x, data[i].top_left.y, data[i].bottom_right.x, data[i].bottom_right.y);

There are three problems regarding fscanf:

  1. fscanf takes pointers, you pass in values
  2. fscanf expects 5 pointers, you only provide 4 values.
  3. format specifier %lf expects a pointer to a double type

Changing the above statement to the follow should solve the crash.

fscanf(fp, "%f %f %f %f %s", &data[i].top_left.x, &data[i].top_left.y, &data[i].bottom_right.x, &data[i].bottom_right.y, data[i].color);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.