如何从 fstream 中准确读取 128 个字节到字符串对象中?
如何从 fstream 中准确读取 128 个字节到字符串对象中?
我编写了一些代码来读取文件的前 128 个字节并打印它,然后读取文件的最后 128 个字节并打印它。最后一部分有效,因为您可以轻松迭代到 EOF,但如何从前面准确获取 128 个字节?下面的代码不起作用,因为您无法将 128 添加到 ifstream 迭代器,它不可索引,只能递增(看起来)。
当然,我可以创建一个迭代器并 *++ 128 次,但必须有一个简单的行方法来完成它,对吧?
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char **argv)
{
std::ifstream ifs ("input.txt",std::ifstream::in | std::ifstream::binary);
if (ifs.good())
{
// read first 128 bytes into a string
ifs.seekg(0,std::ifstream::beg);
std::string first128((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>(ifs))+128);
std::cout << first128 << std::endl;
// read last 128 bytes into a string
ifs.seekg(-128,std::ifstream::end);
std::string last128((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
std::cout << last128 << std::endl;
return 0;
}
return 1;
}
How do I read exactly 128 bytes from an fstream into a string object?
I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since you can't add 128 to an ifstream iterator, it's not indexable, only incrementable (it seems).
Sure I could make an iterator and *++ it 128 times, but there must be a single line straightforward way to do it, right?
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char **argv)
{
std::ifstream ifs ("input.txt",std::ifstream::in | std::ifstream::binary);
if (ifs.good())
{
// read first 128 bytes into a string
ifs.seekg(0,std::ifstream::beg);
std::string first128((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>(ifs))+128);
std::cout << first128 << std::endl;
// read last 128 bytes into a string
ifs.seekg(-128,std::ifstream::end);
std::string last128((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
std::cout << last128 << std::endl;
return 0;
}
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么这个怎么样:
How about this then:
我的答案使用中间缓冲区,但也许您会很高兴它使用迭代器从缓冲区初始化字符串。
对我来说,他们对 iostream 的实现似乎有点太可爱了。尝试使用迭代器进行流 I/O 过于复杂。
顺便说一句,我怀疑您尝试的实现将在幕后执行各种中间缓冲(可能有些在内核中,有些在库中),以及多次重新分配和复制字符串。成长。
另一个想法:您真的需要标准字符串中的结果吗?您可能只是从向量开始工作——避免了复制到字符串的最后一步。或者,如果您喜欢冒险,您可以创建自己的字符串类,它允许您以与向量相同的方式公开内部缓冲区。
My answer uses an intermediate buffer, but perhaps you will be happy that it uses iterators to initialize the string from the buffer.
To me it seems like they got a little too cute with the implementation of iostreams. Trying to use iterators for stream I/O is overly complicated.
By the way, I suspect the implementation that you were attempting will, under the covers, do a variety of intermediate buffering (perhaps some in the kernel, some in the library) as well as re-allocating and copying the string several times as it grows.
One other idea: Do you really need the result in a standard string? You might just work from the vector -- avoiding the final step of copying to a string. Or, if you are feeling adventurous, you could create your own string class that does allow you to expose the internal buffer in the same way that vector does.
在这里,我对流缓冲区进行了一些研究,通过构造函数直接从 istream 读取字符串:
使用示例:
输出:
如果我在某个地方出错,请转发我。
Here i have some of researching over streambuffer, read directly to string from istream by constructor:
Example to use:
Output:
Please forward me if i`am somewhere was wrong.