Search code examples
scanfelementskip

c: skip an element with scanf


I'm trying to scan a file that contains 13 ints which are to be stored in 13 variables. Is there a way to loop over this, while skipping the i-th element? I'm anticipating there might be a solution, which have yet eluded me, perhaps similar to the code below:

int i;
for (i = 0; i < 13; i++)
  fscanf(file, %d, &variables[i]); // somehow apply i to %d

instead of the obvious but lengthy and unclean:

fscanf(file, %d, &variable1);
fscanf(file, %*d, %d, &variable2);
fscanf(file, %*d %*d, %d, &variable3); // etc

thanks


Solution

  • int *variables[] = { &variable1, &variable2, &variable3, ... };
    
    for (int i = 0; i < 13; i++) {
      fscanf(file, "%d", variables[i]);
    }