0

Can I read an array from input buffer directly in reverse order without storing it in a variable (in C++)? It will be helpful when we don't want to store the entire array just to read it in reverse order.

hafiz031
  • 2,236
  • 3
  • 26
  • 48
  • Can you provide example code of what you mean? There are many different ways to have an *array* in `C++` – Galik Apr 01 '18 at 07:52
  • possible duplicate of [Reading in lines from a text file in reverse order c++](https://stackoverflow.com/questions/27752672/reading-in-lines-from-a-text-file-in-reverse-order-c) – moghya Apr 01 '18 at 07:53
  • You can read in an array by iterating over it, assigning the values to the specific array positions directly. For reading in reverse order, just start at the end instead of from the beginning... – Aconcagua Apr 01 '18 at 08:04

1 Answers1

2

You can use fseek(file, 0, SEEK_END) / ftell to find the last element in your input stream and then use fseek to iterate backwards. However, not all kinds of streams support this. Streams associated with files on block devices such as HDD generally do, network sockets don't.

Minor Threat
  • 2,025
  • 1
  • 18
  • 32