从C ++流中读取特定数量的字符到std :: string中 [英] Reading a specific number of characters from C++ stream into std::string

查看:122
本文介绍了从C ++流中读取特定数量的字符到std :: string中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对大多数C ++非常熟悉,但是我避免使用的一个领域是IO流,这主要是因为我一直在不适合使用它们的嵌入式系统上使用它.最近,我不得不熟悉它们,但是我正在努力弄清我认为应该很简单的东西.

I'm pretty familiar with most of C++ but one area I've avoided has been IO streams, mainly because I've been using it on embedded systems where they're not appropriate. Recently I've had to become familiar with them, however, and I'm struggling to figure out something that I feel should be simple.

我正在寻找一种相对有效的方法,以将固定数量的字符从C ++流读取到std::string中.我可以很容易地使用read()方法读取到一个临时的char数组,并将其转换为std::string,但这很丑陋,并且涉及到浪费的副本.我还可以将整个流读取为带有类似以下内容的字符串:

What I'm looking for a relatively efficient way to read a fixed number of characters from a C++ stream into a std::string. I could easily read into a temporary char array with the read() method and convert that into a std::string, but that's fairly ugly and involves a wasteful copy. I could also read the whole of a stream into a string with something like this:

std::string getFile(std::fstream &inFile)
{
    std::stringstream buffer;
    buffer << inFile.rdbuf();
    return buffer.str();
}

...但是对内存的无限制读取通常不是一个好主意,因此我真的很想一次访问一个文件块,比如说4K左右.我也可以一次读取字符,但是与读入临时char数组相比,这既难看又效率低.

... But unbounded reads into memory are generally a poor idea, so I'd really like to access the file one block at a time, say 4K or so. I could also read character at a time, but that just feels both uglier and less efficient than reading into a temporary char array.

那么,是否有一种简单的方法可以直接从包含流中下一个 N 个字符的流中直接获取std::string?很有可能根本没有办法做到这一点,但是对我来说,似乎缺少这样的东西似乎使我感到奇怪,所以我觉得我肯定缺少明显的东西.

So, is there a simple way to get a std::string directly from a stream which contains the next N characters from the stream? It may well be that there is simply no way to do this, but it seems strange to me that such a thing would be missing so I felt I must be missing something glaringly obvious.

顺便说一句,我对C文件IO API相当熟悉,并且我没有在寻找涉及它们的解决方案.我可以用read()write()来解决问题,但是我正在使用的代码大量使用了流,并且我认为保持代码的一致风格是一种很好的做法.

By the way, I'm quite familiar with the C file IO APIs and I'm not looking for a solution involving them. I could knock something up with read() and write(), but the code I'm working with makes heavy use of streams and I think it's good practice to keep my code additions in a consistent style.

推荐答案

您处在正确的轨道上. :)

You are on the right track. :)

mystring.resize( 20 );
stream.read( &mystring[0], 20 );

在C ++ 11中.这是定义明确且安全的. 不同于 data()c_str() 提供对基础数据的 mutable 引用.

In C++11. this is well-defined and safe. Unlike data() and c_str(), std::string::operator[] provides a mutable reference to the underlying data.

在C ++ 03中,从实际意义上讲是安全的,但是std::string的定义太弱了,以至于不能保证这种安全性. 允许C ++ 03 字符串数据不连续,但是我不相信任何可用的实现都可以利用那个.

In C++03, It is safe in a practical sense, but the definition of std::string was too weak to guarantee this safety. C++03 allowed string data to be non-contiguous, but I don't believe any available implementation ever took advantage of that.

这篇关于从C ++流中读取特定数量的字符到std :: string中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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