简体   繁体   中英

Reading an indefinite number of integers from a file

I am in the early stages of coding a homework assignment. The larger goal is a little bit bigger and beyond the scope of this question. The immediate goal is to take one or more two digit numbers from the command line which correspond to years (eg 52). Then open the file that goes with that year. The files are formatted thusly:

    1952 Topps baseball
    -------------------
      8  10  15  17  20  47  48  49  59  71  136
    153 155 159 162 168 170 175 176 186 188 202
    215 233 248 252 253 254 257 259 264 270 271 272 274
    282 283 284 285 287 293 294 295 297 299 300 308 310 311
    312

Each file has a random (between 1-50) number of 1-3 digit integers. I store the year in an int. Then I store each of the later digits into an array. Then I will use that array to do other cool stuff. My problem is, how to I scan for a random number of integer inputs from the file. THis is what I have done so far:

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

    main(int argc, char** argv) {

        char filename[30];
        int cards[100];
        FILE *fp;
        int year,n,i;


        for (i=1; i<argc; i++) {

            n=atoi(argv[i]);

            sprintf (filename,"topps.%d",n);

            if (!(fp=fopen(filename,"r"))){
                printf("cannot open %s for reading\n",filename);
                 exit(3);
            }

            fscanf (fp, "%d%*s%*s%*s%d%d%d%d%d%d%d%d%d%d%d%d",
                    &year,
                    &cards[i],
                    &cards[i+1],
                    &cards[i+2], //this is what needs to be improved upon
                    &cards[i+3],
                    &cards[i+4],
                    &cards[i+5],
                    &cards[i+6],
                    &cards[i+7],
                    &cards[i+8],
                    &cards[i+9],
                    &cards[i+10],
                    &cards[i+11],
                    &cards[i+12]);

            printf ("%d\n",year);
            printf ("%d\n",cards[i+11]);
        }
    }

The current fscanf is just a sort of stopgap to make sure I can read and print the info. It stores up to the 12th integer and prints it. For obvious reasons I didn't want to go to the 50th, because it's pointless. Some files only have 2 or 3 numbers in them. Can anyone help guide me to a more ideal solution for reading files like this? Thanks for having a look.

Something like this does the trick:

Declare 3 new variables at the top:

char sData[10000];
char * pch;
int j = 0;

Then replace your number reading code with the snippet below:

fscanf (fp, "%d%*s%*s%*s", &year);
/* ignore the line with all the dashes (crude, but works)*/
fgets(sData, 10000, fp);
/* read all the number data in */
fgets(sData, 10000, fp);

pch = strtok (sData," ");
j = 0;
while (pch != NULL)
{
    cards[j++] = atoi(pch);
    pch = strtok (NULL, " ");
}

At the end of this code, cards[] should have all your numbers, and j should contain the count.

I greatly appreciate the help I got from everyone. It definitely led me down the right path. However, this is the answer to the problem that eventually worked for me:

fscanf(fp,"%*[^\n]%*c");                             //Skip first two
fscanf(fp,"%*[^\n]%*c");                             //lines of file

while (!feof(fp)) {                                  //Read ints into array
    fscanf(fp,"%d ",&cards[i++]);
}

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