简体   繁体   中英

Looking for a good way to read data from file in C

this is my first question in this site, and I've just started programming, please be patient with me. I'm having some trouble with this code to read strings and intergers from a file, they are separated by a semicolon ";" and it starts with the number of lines. The file is something like this:

13;
A;15;B;1;0;0;0;
A;9;C;0;3;2;0;
A;9;D;0;4;0;2;
A;3;E;2;3;2;0;
A;7;F;5;5;3;1;
A;5;G;5;7;6;0;
A;13;H;0;0;0;0;
A;1;I;8;1;0;0;
A;1;J;2;2;1;0;
A;6;K;7;3;2;0;
A;5;L;2;4;3;0;
A;12;AA;0;3;2;0;
A;9;BA;0;1;0;0;

What I tried to do was to create a function that would receive a file pointer (fp) and the number of lines that was read in the main function. It would read the file and save the intergers and strings in matrices :

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

char timesjogos[100][2][100];
int golsjogos[100][3];
int faltasjogos[100][3];
int camajogos[100][3];
int cverjogos[100][3];

int ReadGames(FILE *caminho,int njogos){
    printf("starting to read jogos.\n");
    int i=0;
    while(fscanf(caminho, " %[^;];%d[^;];%[^;];%d[^;];%d[^;];%d[^;];%d[^;];",
        timesjogos[i][0], &golsjogos[i][0], timesjogos[i][1], &golsjogos[i][1],
        &faltasjogos[i][0], &camajogos[i][0], &cverjogos[i][0]) == 7)
   {
        if(i < njogos)
            i++;
        else
            break;
    }    
}

int main()
{
    FILE *fp;
    int nbets;
    fp = fopen("jogos.txt", "r");
    if (!fp){
        printf ("Error trying to open file.");
    }
    fscanf(fp, " %d[^;];", &nbets);
    ReadGames(fp, nbets);
}

My doubts are about the %[^;]; I used to read each string up to the ; , should I use %d[^;] for the intergers? What is the correct way to do it?

Also, I'm using global variables to save the information read, the problem is that they can be not large enough to save huge amounts of lines (my professor made a 24180 lines file to test our codes). I was thinking about using the number of lines it gives in the first line to make pre-sized matrices inside the function, but how can I return or save it after the function ends?

I'm sorry for the huge code, but I wanted to show all the details. I would be very thankful for your more experienced help :D

The %[^;] notation reads a string consisting of any number of non-semicolons. The parsing stops when a semicolon is encountered. With numbers, the parsing stops at a semicolon anyway; the semicolon is not a part of the representation of a number.

Your use of %d[^;] means that fscanf() is looking for an integer ( %d ), then an open square bracket, caret, semicolon and close square bracket. These don't appear in the input, of course, so the scanning fails.

Therefore, your input loop should probably be:

while (fscanf(caminho, " %[^;];%d;%[^;];%d;%d;%d;%d;",
              timesjogos[i][0], &golsjogos[i][0], timesjogos[i][1],
              &golsjogos[i][1], &faltasjogos[i][0], &camajogos[i][0],
              &cverjogos[i][0]) == 7)
{
    ...
}

You might prefer to specify a maximum length for the %[^;] conversion specifications; %99[^;] would be appropriate since the third dimension of timesjogos is 100. There's an off-by-one difference between the length specified and the length used (enshrined because of ancient history; it was that way before the first C standard, and the C standard codified existing practice).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM