0
A
回答
1
你可以看看你的平台的实现,C++标准或"Standard C++ IOstreams and Locales" by Angelika Langer and Klaus Kreft。
准备一个相当的学习曲线。流是古老而复杂的事情。 (弗朗西斯Glassborow:"I have very few doubts that I/O libraries are amongst the most difficult aspects of any language.")
0
看看STL的实施 sstream 和 sstream.tcc(链接到SGI STL实现)。
基类stringstream
类是basic_stringstream
,它实现了basic_iostream
接口。
// [27.7.4] Template class basic_stringstream
/**
* @brief Controlling input and output for std::string.
*
* This class supports reading from and writing to objects of type
* std::basic_string, using the inherited functions from
* std::basic_iostream. To control the associated sequence, an instance
* of std::basic_stringbuf is used, which this page refers to as @c sb.
*/
有基类basic_stringbuf
从basic_streambuf
派生。这包含缓冲区。
// [27.7.1] template class basic_stringbuf
/**
* @brief The actual work of input and output (for std::string).
*
* This class associates either or both of its input and output sequences
* with a sequence of characters, which can be initialized from, or made
* available as, a @c std::basic_string. (Paraphrased from [27.7.1]/1.)
*
* For this class, open modes (of type @c ios_base::openmode) have
* @c in set if the input sequence can be read, and @c out set if the
* output sequence can be written.
*/
1
Veeeery示意性地,一个“输入”流:
class BufferedInputStream
{
public:
BufferedInputStream(SomeExternalDevice d)
: m_Device(d),
m_Current(m_Buffer),
m_End(m_Buffer)
{}
char get(){
if (!IsDataAvailableInBuffer()){
ReadAChunkFromDiskAndPutItInBuffer();
}
return PopByteFromBuffer();
}
private:
bool IsDataAvailableInBuffer()const{
return m_Current != m_End;
}
void ReadAChunkFromDiskAndPutItInBuffer(){
// Buffer must be empty
assert(!IsDataAvailableInBuffer());
// Read bytes from the device
bytes_read = Read(m_Device, m_Buffer, BufferSize);
// Reposition the "begin" and "end" pointers
m_Current = m_Buffer;
m_End = m_Buffer + bytes_read;
}
char PopByteFromBuffer(){
assert(IsDataAvailableInBuffer());
return *m_Current++;
}
// For example, an OS file handle
SomeExternalDevice m_Device;
// The buffer itself
char m_Buffer[BufferSize];
// Begin of buffer contents
char* m_Current;
// End of buffer contents
char* m_End;
};
这样,数据从磁盘中的缓冲区的大小的组块读出,并且最调用“取得()”不必在调用OS时结束,因为它们可以简单地从缓冲区返回一个字节。
相关问题
- 1. 如何在PHP中实现原型缓存系统?
- 2. 我应该如何在Spring中实现缓存对象/系统?
- 3. 如何在C中实现循环列表(环形缓冲区)?
- 4. C++缓冲流IO
- 5. 如何测试现代系统上的缓冲区溢出?
- 6. 如何在webservice中的C#中实现审计系统?
- 7. C双缓冲区实现死锁?
- 8. 在双缓冲的多线程系统
- 9. Linux缓冲区是如何实现的?
- 10. 如何在缓冲视频时在videoview中实现progressDialog [如youtube]
- 11. 如何缓冲流事件?
- 12. 如何管理流缓冲?
- 13. 流,缓冲在Java中
- 14. 何时为I/O(C++)构建自己的缓冲区系统?
- 15. 级联(缓冲区)实现
- 16. 如何更改Emacs中的缓冲区编码系统?
- 17. 如何在c#中缓冲RichTextBox的缓冲区?
- 18. 如何在SelectListItems列表中实现循环缓冲区?
- 19. Java:如何在Swing中实现双缓冲?
- 20. 如何在android中实现协议缓冲
- 21. 如何在HBase中实现缓冲写入的大小?
- 22. 我将如何在Java中实现Go的无缓冲通道?
- 23. 合并VIM系统缓冲区
- 24. 实现插件/模板系统c#
- 25. c#系统方法&& LINQ实现
- 26. 如何实现日志系统
- 27. Android系统如何实现内部intentservice
- 28. 如何实现公告系统Laravel 5.4
- 29. 如何实现Lisp宏系统?
- 30. 如何实现模块化系统
你是什么意思? stringstream是如何实现的,或者std :: cin/cout? – GManNickG 2009-10-18 09:16:48
缓冲流,就像otl中的流一样 – MemoryLeak 2009-10-18 09:26:17