0

I'm having a problem with some code in my program. I need to read a file and put it's content line by line into a struct. The file is about 800 lines long, and when i try to print my struct, which should now contain the content of the file, it only print about 30 of the lines as they should be. The rest is filed with error or wrong formatting. This is my function as it is now, and i simply call it in main. I am not sure what is wrong but maybe it has something to do with my malloc call?

void read_file(void){
 int lines = count_lines(); /*function to count amount of lines in file*/
 FILE *file;
 int i = 0;
 char filename[] = "race.txt";
 file = fopen(filename, "r");
 race_info *race = malloc(sizeof(race_info));
 if (file != NULL) {
    while (i < lines) {
        fscanf(file, " %[A-Za-z]s %[A-Za-z]s %[A-Z]s %d %[A-Z]s %[A-Z]s %d %d",
               race[i].race_name,
               race[i].name,
               race[i].lastname,
               &race[i].age,
               race[i].team,
               race[i].country,
               &race[i].position,
               &race[i].time);
        i++;
    }
 }
 else {
    perror(filename); //print the error message
 }
 for (i = 0; i < lines; i++) {
    printf("%s %s %s %d %s %s %d %d",
          race[i].race_name,
          race[i].name,
          race[i].lastname,
          race[i].age,
          race[i].team,
          race[i].country,
          race[i].position,
          race[i].time);
 }
 fclose(file);
} 

The struct is setup as following:

#define MAX_CHAR 100



struct race_info{
  char race_name[MAX_CHAR];
  char name[MAX_CHAR];
  char lastname[MAX_CHAR];
  int age;
  char team[MAX_CHAR];
  char country[MAX_CHAR];
  int position;
  int time;
};
typedef struct race_info race_info;

The lines from the file is setup as:

RaceName               "Name LASTNAME"                                AGE  TEAM   Country      Position      TIME

The goal is to print the struct so that all 800 lines are printed with the same formatting as the file. But when printed it does only prints about 200 lines and and it does not go from start of file to the end, but takes content from the middle of it. A lot of the lines also have wrong formatting.

example of running program

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
CHr_fred
  • 3
  • 2
  • Provide an example of input and output please. What is `count_lines()` ? Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – Stargateur Nov 25 '17 at 15:18
  • 1
    You need to check the return value from `fscanf` and do something if it fails. As the code stands, if any one of those calls fails, due to unexpectedly-formatted input, it's going to get "stuck" there, trying and failing to read the same improper input, never getting to the rest of the file. – Steve Summit Nov 25 '17 at 15:49
  • 1
    Formats like `%[A-Za-z]s` are probably wrong. I suspect you want `%[A-Za-z]`, without the `s`. See [this recent answer](https://stackoverflow.com/questions/47445131/how-to-read-string-separated-by-with-scanf/47445307#47445307). – Steve Summit Nov 25 '17 at 15:54

1 Answers1

2

This race_info *race = malloc(sizeof(race_info)); seems to only allocate space for a single race_info.

You should probably have malloc(lines * sizeof(race_info)) for all the lines to fit.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Thanks for the response. I tried it but it just printed a lot of zeroes instead of text. – CHr_fred Nov 25 '17 at 15:35
  • 2
    Then there might be some other problem, perhaps how `count_lines()` get the line count? Or if some name doesn't fit the `%[A-Za-z]s` format? – Bo Persson Nov 25 '17 at 15:48
  • I do not think there is a problem with those two, count_lines() just return an integer, and all the names fit in with the format. I talked to a friend a he said its probably something to do with my memory allocation, and he said i should try using static allocation. But i am not sure how i should do that, and how much memory i would need. – CHr_fred Nov 25 '17 at 19:39