문제

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

도움이 되었습니까?

해결책

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

for (int i = 0; i < 13; i++) {
  fscanf(file, "%d", variables[i]);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top