1

I have a .txt file that contains the data on a moving object in the following format

mass size velocity(x) velocity(y) velocity(z) etc

In addition, this file contains some background information denoted by a character, so

M 1000

C 500

I've been trying to write code that reads through and sorts the data according to what is (so M is stored in one variable, C in another, and the data for the object stored in its correct arrays). Currently, my code segmentation faults when I compile it. I've tried re-writing it with fgets, fscanf, gets and in every other way I can, but nothing is working.

while(fgets(data, 50, file))
{
    if (data[0] == "M")
    {
        M = data[1];
    }
    else
    {
        fscanf(data,
            "%lf %lf %lf %lf %lf",
            obj_stat[0],
            obj_stat[1],
            obj_stat[2],
            obj_stat[3],
            obj_stat[4],
            obj_stat[5]);
    }
}


int fclose(univ);
Community
  • 1
  • 1

1 Answers1

2

I'm going to take some guesses here because your code is incomplete. But you are trying to use fscanf on what appears to be a string. That's one mistake.

Your fscanf() also has 5 format specifiers, but you are passing six arguments. And you forgot to take their addresses with & in every case.

The way you are treating the M line is also incorrect. You should be using sscanf() in both cases.

giusti
  • 3,156
  • 3
  • 29
  • 44