0

So, I'm trying to get my program to read into an array of structs from a text file, and it compiles okay, but doesnt appear to actually be reading in the values?.. and I have no idea why. This is the relevant part of the code:

typedef struct Planet
{
char Planet_Name[30];
double Fuel;
double Velocity;
double Height;
double Gravity;
int Maximum_Thrust;
double Difficulty;
}Planet;


    //read the Planets from a file
    FILE* inputFile = fopen("Planets.txt", "r");
    if(inputFile == NULL)
    {
        perror("Error. File unavailable");
        exit(1);
    }

    for(j=0; j<10; j++)
    {   
        fscanf("%29s %lf %lf %lf %lf %d %lf", SolarSystem[j].Planet_Name, 
        SolarSystem[j].Fuel, SolarSystem[j].Velocity, 
        SolarSystem[j].Height, SolarSystem[j].Gravity, 
        SolarSystem[j].Maximum_Thrust, SolarSystem[j].Difficulty);
    }

    printf("Please select a planet by entering the corresponding number: 
    Mercury[0], Venus[1], Earth[2], Moon[3], Mars[4], Jupiter[5], Saturn[6], 
    Uranus[7], Neptune[8]\n");

    scanf("%d",&PlanetNum);

    printf("You have chosen %s", SolarSystem[PlanetNum].Planet_Name);

This is the txt file (Title: Planets.txt)

Mercury 120 50 500 12.1 30 2 Venus 120 50 500 29.1 30 6 Earth 120 50 500 32.2 30 7 Moon 120 15 50 5.3 30 2 Mars 120 50 500 12.2 30 4 Jupiter 120 50 500 81.3 30 10 Saturn 120 50 500 34.3 30 8 Uranus 120 50 500 28.5 30 5 Neptune 120 50 500 36.6 30 9 Pluto 120 50 500 2.03 30 1

Except when it runs that final printf, it doesn't actually print anything, nor does it store any data in the structs (when its called later it's all zeroes). Ideas?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Liam
  • 3
  • 3
  • Please see: [ask] and consider preparing a [mcve]. – Paul R May 24 '18 at 15:21
  • Accept answer if you are satisfied . – anoopknr May 24 '18 at 15:57
  • I have another question.. But the website says I have to wait two days to post again..wtf? Am i right to just paste it in below the above?.. – Liam May 24 '18 at 17:09
  • where do you get stuck.? Is in writing to file..? – anoopknr May 24 '18 at 18:14
  • 1
    @Liam: I’ve rolled your question back to its original form. If you have a new question then it needs to be posted separately, as a new question. You should upvote/accept the answer below if it helped you. This will also help to build up your reputation and unlock priveleges on the site. – Paul R May 25 '18 at 08:33
  • How do I accept it?.. And I'm not exactly here for the long run haha As soon as I'm done with this silly subject I'm out.. Can't wait to be rid of it. – Liam May 25 '18 at 09:57
  • @Liam: there is an outline of a check-mark (aka "tick" in proper English) under the up-vote/down-vote buttons next to the answer. Click on this and it should turn green. You might also want to give the answerer an up-vote while you're there, to show your appreciation for their help. – Paul R May 25 '18 at 10:04
  • 1
    @PaulR Thanks! I'm just blind. Apparently my upvotes dont count btw – Liam May 25 '18 at 14:40

1 Answers1

1

The mistake is with your fscanf function . You have to provide FILE pointer (inputFile This context) as first argument and & operator(address of Similar to scanf function) in front of scanning integer and float.

Try this modified fscanf code :-

fscanf(inputFile,"%s%lf%lf%lf%lf%d%lf",SolarSystem[j].Planet_Name,&SolarSystem[j].Fuel, &SolarSystem[j].Velocity, &SolarSystem[j].Height, &SolarSystem[j].Gravity,&SolarSystem[j].Maximum_Thrust, &SolarSystem[j].Difficulty);
Paul R
  • 208,748
  • 37
  • 389
  • 560
anoopknr
  • 3,177
  • 2
  • 23
  • 33
  • Ah bugger.. I Just rechecked the tutorial video I'm watching, he sneaks it in without making a comment, thats why I missed it! I must ask, how do you know where to put those & symbols? I usually just play around with them in my variables until the program works properly.. – Liam May 24 '18 at 15:39
  • 1
    In simple words other that read `string` (character array) most probably you need to use `&` operator. – anoopknr May 24 '18 at 15:43
  • @Liam [when to use ampersands and asterisks in C](https://stackoverflow.com/questions/2094666/pointers-in-c-when-to-use-the-ampersand-and-the-asterisk) – Tormund Giantsbane May 24 '18 at 17:45
  • @TormundGiantsbane I had a read.. It didn't really make much sense to be honest. Programming just isnt my thing. I cant stand it – Liam May 24 '18 at 17:55
  • @anoopknr: I’ve rolled back your answer to the previous version, to make it consistent with the OP’s original question (before the OP radically changed it). – Paul R May 25 '18 at 08:39