1

I really hope someone is up for the task of helping, or pointing me to ressources that can, because I've been struggling with this for hours now.

I do not have a lot of experience in C yet, or coding in general,and I have this assignment where I need to read a txt file with soccer/football match results into an array, and I can't get the "fscanf" part to work.

I've created a struct with the details I need to save and work with later. (right now the date is just saved as a string, alternatively I could save that information as 2 integers DD MM).

I'm calling my "file_read" from main (int function, as I need to know how many lines read later on for qsort), and passing my "struct type" array and .txt file as parameter to that function.

In the "file_read" function I have an int variable for counting, and opening the file specified in main, checking for NULL, and then "getting to it" by calling the function "read_match" and passing my file pointer as parameter.

THIS IS WHERE I'M HAVING TROUBLE (maybe earlier according to the more code savvy)!

I can't figure out how to distinguish the various segments(?) of the textfile, using the %s and whatnot, to be able to properly save it in the various struct types.

The data in the txt file looks like this:

  (day)   (date)(time)   (teamA)(teamB) (goalsA)(goalsB)  (spectators)
    Son     28/07 18.00     BIF - OB      3 - 2     13689  
    Man     29/07 19.00     AaB - SIF     3 - 1     5885   
    Fre     02/08 19.00     SIF - HOB     2 - 3     3468 

(added struct names to the data above)

And my entire code, so far, looks like this: (scroll to bottom)

(I tried various approaches like %[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ] %[0123456789] and what you see below, but it always seems to go bad after the first "-" in the txt file. There's a print function added for troubleshooting)

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

#define STR_SHORT 10
#define STR_LONG 70
#define MATCHES 300

typedef struct {
    char day[STR_SHORT];
    char date[STR_SHORT];
    char time[STR_SHORT];
    char team_A[STR_SHORT];
    char team_B[STR_SHORT];
    int goals_A;
    int goals_B;
    int spectators;
} Match;

int file_read(char *file_input, Match arr_matches[]);
Match read_match(FILE *file_ptr);

/*MAIN*/
int main(void) {
    Match arr_matches[MATCHES];
    int n;
    n = file_read("data.txt", arr_matches);
    printf(" %d", n);
    
    return 0;
}

/*FUNCTIONS*/

int file_read(char *file_input, Match arr_matches[]) {
    int r = 0;
    FILE *file_ptr = fopen(file_input, "r");

    if (file_ptr == NULL) {
        printf("Error! Can't open file!");
        return 1;
    }

    while(!(feof(file_ptr))){
       arr_matches[r] = read_match(file_ptr);
       ++r;
    }
    fclose(file_ptr);
    return r;
}

Match read_match(FILE *file_ptr) {
    Match input;

    fscanf(file_ptr,    " %s %s %s"
                        " %s  %7s"
                        " %2d %*[-] %2d"
                        " %d",
                   input.day, input.date, input.time, 
                   input.team_A, input.team_B,
                   &input.goals_A, &input.goals_B,
                   &input.spectators);
  printf("(day) %s (date) %s (time) %s (teamA) %s (teamB) %s (scoreA) %d (scoreB) %d (spect) %d\n", 
  input.day, input.date, input.time, input.team_A, input.team_B, input.goals_A, input.goals_B, input.spectators);
  return input;
}

Usable feedback would be much appreciated, and ofc, let me know if I need to elaborate on something.

Thanks!


Edited: Been trying out STRTOK, I might be able to combine this somehow...

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

int main () {
   char str[80] = "Fre     12/07 19.00     FCM - EFB     1 - 0     7310   ";
   const char s[] = "-' '";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}
MMAndersen
  • 11
  • 3

1 Answers1

0

How to restrict fscanf properly on a string from a txt file? (need to read strings and integers separated with white-space AND "-")?

The key part missing in OP's format is the 1st '-' and failure to test the return value.

 // Son     28/07 18.00     BIF - OB      3 - 2     13689  
 // fscanf(file_ptr,    " %s %s %s" " %s  %7s" " %2d %*[-] %2d" " %d", ...
 int cnt = fscanf(file_ptr, "%s%s%s" "%s -%7s" "%2d -%2d" "%d", 
 //                                      ^
 if (cnt == 8) Success();

Also review Why is “while ( !feof (file) )” always wrong?


Tip: Rather than reading the line of data with fscanf(), use fgets().

char buf[100];
if fgets(buf, sizeof buf, file_ptr) == NULL) {

Then parse the line of input with sscanf(), strtol(), etc.


Better to use width limits with "%s"

int cnt = fscanf(file_ptr, "%9s%9s%9s" "%9s -%9s" "%2d -%2d" "%d", 

Decide how to 1) handle no more input, 2) errant input.


Some untested code that puts together these ideas:

It also uses " %n" to detect junk after the good portion of an input line.

Match* read_match(FILE *file_ptr, Match *input) {
  char buf[100];
  if (fgets(buf, sizeof buf, file_ptr) == NULL) {
    return NULL;
  }
  int n = 0;
  if (sscanf(buf,
      "%9s%9s%9s" "%9s -%9s" "%2d -%2d" "%d" " %n", //
      input->day, input->date, input->time, //
      input->team_A, input->team_B, //
      &input->goals_A, &input->goals_B, //
      &input->spectators, //
      &n) != 8 || buf[n]) {
    fprintf(stderr, "Invalid input \"%s\"\n", buf);
    return NULL;
  }
  return input;
}

int file_read(char *file_input, int n, Match *arr_matches) {
  int r = 0;
  FILE *file_ptr = fopen(file_input, "r");

  if (file_ptr == NULL) {
    printf("Error! Can't open file!");
    return -1;
  }

  int i;
  for (i = 0; i < n; i++) {
    if (read_match(file_ptr, &arr_matches[i]) == NULL) {
      break;
    }
  }
  fclose(file_ptr);
  return i;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Thank you for the feedback @chux! I will make sure to read tomorrow mornign and try to understand, but atleast the tip about "Better to use width limits with "%s" int cnt = fscanf(file_ptr, "%9s%9s%9s" "%9s -%9s" "%2d -%2d" "%d", " helped (according to the printf I added for troubleshooting) – MMAndersen Nov 28 '20 at 22:39