0

I have a data file and I want to read it into a struct.

This is the contents of the data file

Japan 46.2 16 12.7
Spain 42.8 18.5 39.3
Italy 53.25 19.8 32.8
France 54.5 21.1 31.4
Turkey 52.5 15.6 19.1

This is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    

    struct covid
    {
        char location[100];
        double does_given;
        double full_vaccinated;
        double of_population_fully_vaccinated;
    };

    FILE *infile;
    infile=fopen("test.txt","r");
    
    if (infile == NULL)
    {
        fprintf(stderr, "\nError opening file\n");
        exit (1);
    }

    struct covid stats;
    
    while (fread(&stats,sizeof(struct covid),1,infile)){
        printf("name =%s, give =%f, full=%f, pop=%f\n",stats.location, stats.does_given, stats.full_vaccinated, stats.of_population_fully_vaccinated);
        
    };

    fclose(infile);
    return 0;
    
}

However, when I run this code, I get no output. Why doesn't it work?

7
  • 3
    As for your problem, you try to read raw binary structures from a text file, which will not work. You need to read the file line by line, and then parse each line to get its value to put into structures. Hint: fgets and sscanf could be useful. Commented Jan 11, 2022 at 8:23
  • @Gerhardh Can i use .dat file? Commented Jan 11, 2022 at 8:25
  • 1
    Now COVID managed to infect even programming languages... :-D Commented Jan 11, 2022 at 8:26
  • 2
    You can name your file whatever you want, it's the actual contents and its format that dictates how you need to read and parse the file. If it's a text file where each line in a single record, it doesn't matter if the file is named foo.txt, foo.dat or foo.bar, you must still read it line by line and parse each line. Commented Jan 11, 2022 at 8:27
  • 1
    A .dat file can mean anything or nothing. You show that your file contains human readable text. This does not match any struct layout. Commented Jan 11, 2022 at 8:28

1 Answer 1

1

Your file contains textual representations of numbers, you cannot blindly read that text into a struct, there is no magic that will transform the textual representation into doubles.

You need to read the file line by line and parse each line individually.

You want something like this:

  char line[1000];

  while (fgets(line, sizeof(line), infile)) {
    sscanf(line, "%s %lf %lf %lf", stats.location, &stats.does_given,
                  &stats.full_vaccinated, &stats.of_population_fully_vaccinated);

    printf("name =%s, give =%f, full=%f, pop=%f\n", stats.location, stats.does_given,
            stats.full_vaccinated, stats.of_population_fully_vaccinated);
  };

Disclaimer: there is no error checking whatsoever.

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.