Pregunta

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

¿Fue útil?

Solución

int *variables[] = { &variable1, &variable2, &variable3, ... };

for (int i = 0; i < 13; i++) {
  fscanf(file, "%d", variables[i]);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top