如何修改std :: cin的内部缓冲区 [英] How do I modify the internal buffer of std::cin
问题描述
我正在编写一个使用 std :: cin
但是不太可能,我试图避免密码从内存中分页到磁盘的可能性,所以我想修改 std :: cin
的缓冲区以尽快覆盖密码完成了.
However unlikely, i am trying to avoid the possibility that the password get paged to the disk from memory so I want to modify the buffer of std::cin
to overwrite the password as soon as I'm done with it.
现在我有这个:
std::cin.clear();
std::stringstream ss;
ss << "0000000000000000000000000000000000000000000000";
std::cin.rdbuf(ss.rdbuf());
std::cin.clear();
但是我很确定这很不好,因为它没有考虑到cin缓冲区的当前大小.如何正确覆盖缓冲区的内容?
but I'm pretty sure this is bad since it doesn't take into account the current size of the cin buffer. How do i properly overwrite the contents of the buffer?
感谢您的帮助!
推荐答案
您可以使用 gptr()
和 egptr()
来获取缓冲区的开始和结束
You can use gptr()
and egptr()
to get the beginning and end of the buffer.
正如查尔斯·贝利(Charles Bailey)指出的那样,这些都受到保护.我的假设是,如果您想要一个流缓冲区,可以在指定的时间清除其内容,那么您将实现自己的一个,该派生自标准流缓冲区类之一,但提供了 clear()
成员(或您觉得方便的任何名称).在缓冲区管理器不知情的情况下更改缓冲区的内容通常是一件很糟糕的事情……
As Charles Bailey pointed out, these are protected. My assumption is that if you want a stream buffer that you can clear its contents at a specified time, that you'd be implementing one of your own that derives from one of the standard stream buffer classes, but provides a clear()
member (or whatever name you find convenient). Changing the contents of the buffer without the buffer manager knowing about it will generally be a rather bad thing...
这篇关于如何修改std :: cin的内部缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!