Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,

How to read the file from end position to beginning position using file stream reverse iterator?

Anybody answer my question............
Posted
Updated 19-Aug-10 7:52am
v2
Comments
LittleYellowBird 19-Aug-10 7:23am    
Hi, adding more question marks does not encourage people to answer more quickly, in fact it probably makes you look pushy. Just a suggestion. :)
Abhinav S 19-Aug-10 13:52pm    
Removed question marks..........

The quickest way I can think of to get a istream_reverse_iterator would be to use an ordinary istream_iterator but implement your own stream buffer for the stream you're basing it on.

Have a look at Kreft and Langer's "Standard C++ Streams and Locales" for all the gory details of the iostream libraries and why what you want is a bit tricky. It'll also tell you how to implement your own stream buffers to help you out of these situations.

Cheers,

Ash
 
Share this answer
 
Comments
Nuri Ismail 19-Aug-10 8:27am    
Reason for my vote of 5
Great reference! IMO "Standard C++ Streams and Locales" is the best resource for everyone who wants to go deeper in the standard C++ iostream library. :)
A non-professional solution :) :
class mybuffer: public basic_filebuf<char>
{
public:
  char* Beg() { return gptr(); }
  char* End() { return egptr(); }
};

int _tmain(int argc, _TCHAR* argv[])
{
  std::ifstream file;
  file.open(_T("d:\\test.txt"));
  
  mybuffer* Buf = (mybuffer*) file.rdbuf(); // a "way" to the protected methods
  Buf->sgetc(); // a "way" to fill the buffer (the file will be read)

  char* pCharBeg = Buf->Beg();
  char* pCharEnd = Buf->End();

  reverse_iterator<char*> rIter(pCharEnd);

  while (pCharBeg <= &(*rIter)) {
    cout << *(rIter++);
  }

  file.close();
  
  return getchar();
}
 
Share this answer
 
v2
Comments
Aescleal 19-Aug-10 18:52pm    
You can make this a bit less fiddly with a couple of helper functions. However there's a big problem in that underflow isn't triggered automatically when the buffer is empty. So if the file you're adapting is greater than whatever implementation specific size the streambuf uses you're not going to get the whole file.
Eugen Podsypalnikov 20-Aug-10 2:18am    
Yes, it was my guess too (regarding the size) :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900