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);
}