Will not read from file to structure

I have been sitting with this problem for 2 days and I can’t understand what I am doing wrong. I tried debugging (like? Not yet new) and then this link: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ And I tried Google and all kinds of things. I mainly read from a file with this format:

R1 Fre 07/07/2015 18.00 FCN - SDR 0 - 2 3.211

and I have to get the program to read this in the structure, but when I try to print the information, it gives everything wrong. My code is as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_INPUT 198

typedef struct game{
    char   weekday[4],
           home_team[4],
           away_team[4];
    int    round,
           hour,
           minute,
           day,
           month,
           year,
           home_goals,
           away_goals,
           spectators;}game;

game make_game(FILE *superliga);

int main(void){
    int    input_number,
           number_of_games = 198,
           i = 0;
    game   tied[MAX_INPUT];

    FILE *superliga;
    superliga = fopen("superliga-2015-2016.txt", "r");

    for(i = 0; i < number_of_games; ++i){
                tied[i] = make_game(superliga);
                printf("R%d %s %d/%d/%d %d.%d %s - %s %d - %d %d\n",
                        tied[i].round, tied[i].weekday, tied[i].day, tied[i].month,
                        tied[i].year, tied[i].hour, tied[i].minute, tied[i].home_team,
                        tied[i].away_team, tied[i].home_goals, tied[i].away_goals,
                        tied[i].spectators);}

 fclose(superliga);

 return 0;
}

game make_game(FILE *superliga){
    double spect;
    struct game game_info;

    fscanf(superliga, "R%d %s %d/%d/%d %d.%d %s - %s %d - %d %lf\n",
            &game_info.round, game_info.weekday, &game_info.day, &game_info.month,
            &game_info.year, &game_info.hour, &game_info.minute, game_info.home_team,
            game_info.away_team, &game_info.home_goals, &game_info.away_goals,
            &spect);

         game_info.spectators = spect * 1000;

    return game_info;
 }
+4
source share
2 answers

, , .

( , : ​​ , . , , , , x y z, x, y, z, x/y/z, (x,y,z), [x,y,z], <x y z>, <x,y,z> .., , . scanf , .)

, fgets() . . POSIX.1 (.. Windows), getline(), , - .

, sscanf().

, , scanf : . , \n " "; , , ", , ". , %c %[, ; , , , , .

scanf . ( "" %n, , , .) , EOF.

- , , , , %*s -, . , , sscanf(line, " %*d %*s %*d") 3, , (, ), .

, , ( ) . 0 , .

,

#ifndef  GAME_LINE_MAX
#define  GAME_LINE_MAX   1022
#endif

int read_game(game *one, FILE *in)
{
    char  buffer[GAME_LINE_MAX + 2]; /* + '\n' + '\0' */
    char *line;

    /* Sanity check: no NULL pointers accepted! */
    if (!one || !in)
        return -1;

    /* Paranoid check: Fail if read error has already occurred. */
    if (ferror(in))
        return -1;

    /* Read the line */
    line = fgets(buffer, sizeof buffer, in);
    if (!line)
        return -1;

    /* Parse the game; pattern from OP example: */
    if (sscanf(line, "R%d %3s %d/%d/%d %d.%d %3s - %3s %d - %d %d\n",
                     &(one->round), one->weekday,
                     &(one->day), &(one->month), &(one->year),
                     &(one->hour), &(one->minute)
                     one->home_team,
                     one->away_team,
                     &(one->home_goals),
                     &(one->away_goals),
                     &(one->spectators)) < 12)
        return -1; /* Line not formatted like above */

    /* Spectators in the file are in units of 1000; convert: */
    one->spectators *= 1000;

    /* Success. */
    return 0;
}

, (stdin):

game  g;

while (!read_game(&g, stdin)) {

    /* Do something with current game stats, g */

}

if (ferror(stdin)) {

    /* Read error occurred! */
else
if (!feof(stdin)) {

    /* Not all data was read/parsed! */

}

if , (, - ), / ( ), .

OP: -, , 3 + 1 . '\0', %s. -, 1000, .

, one->weekday, one->home_team one->away_team . , , . ( char a[5];, a &a &(a[0]) a). " " , %s .

0

. , R, .

fscanf(), , .

fscanf(), :

fscanf(superliga, " R%d %s %d/%d/%d %d.%d %s - %s %d - %d %lf\n",
            &game_info.round, game_info.weekday, &game_info.day, &game_info.month,
            &game_info.year, &game_info.hour, &game_info.minute, game_info.home_team,
            game_info.away_team, &game_info.home_goals, &game_info.away_goals,
            &spect);
0

Source: https://habr.com/ru/post/1662318/


All Articles