0

I have a small file containing numbers, separated by new lines/spaces. I am looking for a way to scan the file , but in reverse.

Input: 1 2 3 4 5 1025
Output: 1025 5 4 3 2 1

Attention! in the following code, I need to modify the scanf. I need to leave printf unchanged! I don't need to transform 1024 into 4201, the integers remain unchanged. Only their order changes.

Code:

while (fscanf(input,"%d", &b) && !feof(input)) {printf("%d",b);}

1 Answers1

2

Do you have a big enough stack?

void printreverse(FILE *f) {
    int n;
    if (fscanf(f, "%d", &n) != 1) return;
    printreverse(f); // recursive call
    printf("%d", n);
}

https://ideone.com/1jejpM

pmg
  • 106,608
  • 13
  • 126
  • 198