文件中的反向迭代器 [英] Reverse iterator in file

查看:98
本文介绍了文件中的反向迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何使用文件流反向迭代器从结束位置到开始位置读取文件?

有人回答我的问题............

Hello to all,

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

Anybody answer my question............

推荐答案

我想到的获得istream_reverse_iterator的最快方法是使用普通的istream_iterator,但为基于它的流实现您自己的流缓冲区.

看看Kreft和Langer的"Standard C ++ Streams and Locales",了解iostream库的所有细节,以及为什么想要的东西有些棘手.它还会告诉您如何实现自己的流缓冲区,以帮助您摆脱这些情况.

干杯,

Ash
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


非专业解决方案:):
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();
}


这篇关于文件中的反向迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆