0

Is there a way to pipe a file of (bytes- obviously) in to a C++ application on Linux. However, rather than just use getline() I would like to read each byte only once. For example I don't want to use getline() because it would read all the bytes up to the '\n' and then I would also have to re-read the bytes given to me via getline(), so the bytes are read twice. I would just like to "iterate" through each byte once.

What would be the best-performing technique, to read PAGE_SIZE bytes at a go? Any example code is most welcome!

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
user997112
  • 29,025
  • 43
  • 182
  • 361

1 Answers1

4

Don't forget that std::cin is of type std::istream. You can use standard get() on it to retrieve a char at a time with:

char nextVal = std::cin.get();

To read PAGE_SIZE bytes at a go, use read() instead:

char *buffer = new char[PAGE_SIZE];
std::cin.read(buffer, PAGE_SIZE);

Remember to always check error conditions and EOF after reading.

Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • However, this is manually implementing what Linux memory-mapped files achieve. So if performance is an issue, that may well be a better approach. – Oliver Charlesworth Oct 16 '13 at 20:58
  • Thanks for this- would you agree reading PAGE_SIZE bytes at a time would be the most efficient? Would there be a "neat" way of handling the last read (where there wouldnt necessarily be PAGE_SIZE bytes remaining)? – user997112 Oct 16 '13 at 20:58
  • @OliCharlesworth agreed, but I have been told the input needs to be "piped" in to the application from (presumably) terminal. – user997112 Oct 16 '13 at 20:59
  • Yes, it is more efficient, because it translates to a single system call to read, instead of `PAGE_SIZE` separate ones. To handle the last read, you can use `readsome()` instead of `read()`: it returns the number of bytes actually stored in the array: on the last call, it will be less than `PAGE_SIZE`. – Stefano Sanfilippo Oct 16 '13 at 21:01
  • Any response regarding the other answer here, saying that read() would introduce another buffer? – user997112 Oct 16 '13 at 21:19