70

我通常stringstream用来写入内存中的字符串。有没有办法以二进制模式写入字符缓冲区?考虑以下代码:

stringstream s;
s << 1 << 2 << 3;
const char* ch = s.str().c_str();

内存ch将如下所示: 0x313233 - 字符 1、2 和 3 的 ASCII 码。我正在寻找一种自己编写二进制值的方法。也就是说,我希望内存中有 0x010203。问题是我希望能够编写一个函数

void f(ostream& os)
{
    os << 1 << 2 << 3;
}

并决定外部使用哪种流。像这样的东西:

mycharstream c;
c << 1 << 2 << 3; // c.data == 0x313233;
mybinstream b;
b << 1 << 2 << 3; // b.data == 0x010203;

有任何想法吗?

4

7 回答 7

44

要将二进制数据读取和写入流,包括字符串流,请使用 read() 和 write() 成员函数。所以

unsigned char a(1), b(2), c(3), d(4);
std::stringstream s;
s.write(reinterpret_cast<const char*>(&a), sizeof(unsigned char));
s.write(reinterpret_cast<const char*>(&b), sizeof(unsigned char));
s.write(reinterpret_cast<const char*>(&c), sizeof(unsigned char));
s.write(reinterpret_cast<const char*>(&d), sizeof(unsigned char));

s.read(reinterpret_cast<char*>(&v), sizeof(unsigned int)); 
std::cout << std::hex << v << "\n";

这给0x4030201了我的系统。

编辑:为了使用插入和提取运算符(<< 和 >>)使这项工作透明,您最好创建一个派生的 streambuf 来做正确的事情,并将其传递给您想要使用的任何流。

于 2009-10-13T11:14:30.430 回答
12

你可以用模板做这种事情。例如:

//struct to hold the value:
template<typename T> struct bits_t { T t; }; //no constructor necessary
//functions to infer type, construct bits_t with a member initialization list
//use a reference to avoid copying. The non-const version lets us extract too
template<typename T> bits_t<T&> bits(T &t) { return bits_t<T&>{t}; }
template<typename T> bits_t<const T&> bits(const T& t) { return bits_t<const T&>{t}; }
//insertion operator to call ::write() on whatever type of stream
template<typename S, typename T>
S& operator<<(S &s, bits_t<T> b) {
    return s.write((char*)&b.t, sizeof(T));
}
//extraction operator to call ::read(), require a non-const reference here
template<typename S, typename T>
S& operator>>(S& s, bits_t<T&> b) {
    return s.read((char*)&b.t, sizeof(T));
}

它可以使用一些清理,但它的功能。例如:

//writing
std::ofstream f = /*open a file*/;
int a = 5, b = -1, c = 123456;
f << bits(a) << bits(b) << bits(c);

//reading
std::ifstream f2 = /*open a file*/;
int a, b, c;
f >> bits(a) >> bits(b) >> bits(c);
于 2018-02-06T12:24:33.310 回答
4

好吧,只使用字符,而不是整数。

s << char(1) << char(2) << char(3);
于 2009-10-13T10:05:45.313 回答
3

重载一些不寻常的运算符效果很好。在下面我选择重载<=因为它具有与<<相同的从左到右的关联性,并且在某种程度上具有接近的外观和感觉......

#include <iostream>
#include <stdint.h>
#include <arpa/inet.h>

using namespace std;

ostream & operator<= (ostream& cout, string const& s) {
    return cout.write (s.c_str(), s.size());
}
ostream & operator<= (ostream& cout, const char *s) {
    return cout << s;
}
ostream & operator<= (ostream&, int16_t const& i) {
    return cout.write ((const char *)&i, 2);
}
ostream & operator<= (ostream&, int32_t const& i) {
    return cout.write ((const char *)&i, 4);
}
ostream & operator<= (ostream&, uint16_t const& i) {
    return cout.write ((const char *)&i, 2);
}
ostream & operator<= (ostream&, uint32_t const& i) {
    return cout.write ((const char *)&i, 4);
}

int main() {
    string s("some binary data follow : ");

    cout <= s <= " (machine ordered) : " <= (uint32_t)0x31323334 <= "\n"
         <= s <= " (network ordered) : " <= htonl(0x31323334) ;
    cout << endl;

    return 0;
}

有几个缺点:

  • <=的新含义可能会使读者感到困惑或导致意想不到的结果:

    cout <= 31 <= 32;
    

    不会给出相同的结果

    cout <= (31 <= 32);
    
  • 如上例所示,在阅读代码时并未明确提及字节序。

  • 它不能简单地与<<混合,因为它不属于同一组优先级。我通常使用括号来澄清,例如:

    ( cout <= htonl(a) <= htonl(b) ) << endl;
    
于 2013-03-15T17:39:51.433 回答
2

对于这个用例,我自己实现了一个“原始移位运算符”:

template <typename T, class... StreamArgs>
inline std::basic_ostream<StreamArgs...> &
operator <= (std::basic_ostream<StreamArgs...> & out, T const & data) {
        out.write(reinterpret_cast<char const *>(&data), sizeof(T));
        return out;
}

把它放在方便的地方并像这样使用它:

std::cout <= 1337 <= 1337ULL <= 1337. <= 1337.f;

优点:

  • 可链式
  • 自动的sizeof()
  • 也接受数组和结构/类实例

缺点:

  • 对非 POD 对象不安全:泄漏指针和填充
  • 输出是平台特定的:填充、字节序、整数类型
于 2017-01-13T13:11:48.743 回答
0

我真的很喜欢韩洛的方法,并且已经验证了它的效果很好!如果将 oss 成员变量更改为使用 std::stringstream (与 ostringstream 相比),该类也可用于使用重载流提取运算符进行提取,如下所示:

    template <typename T, typename std::enable_if<std::is_fundamental<T>::value, bool>::type = true>
    bostringstream& operator>> (T& v)
    {
        char buffer[sizeof(T)];
        oss.read(buffer, sizeof(T));
        v = *(T*)buffer;
        return *this;
    }

示例模板支持整数类型,如果为 std::is_compound 添​​加新模板,则还可以支持 std::map 等类型。对于 std::vector 之类的东西,即“is_fundemental”,我建议先将大小推送到流中,因此在提取端可以拉出它以了解之后要拉出多少元素。这种方法可以很好地与常见的 std::vector 和 std::map 类型一起使用。

于 2021-12-03T14:45:32.820 回答
0
#include <sstream>

class bostringstream {
public:
  bostringstream() : oss() {}

  template <typename T, typename std::enable_if<std::is_fundamental<T>::value,
                                                bool>::type = true>
  bostringstream& operator<<(const T& v) {
    oss.write((char*)&v, sizeof(T));
    return *this;
  }

  template <typename T, typename std::enable_if<
                            std::is_fundamental<typename T::value_type>::value,
                            bool>::type = true>
  bostringstream& operator<<(const T& v) {
    oss.write((char*)v.data(), v.size() * sizeof(typename T::value_type));
    return *this;
  }

  template <typename _InputIterator>
  bostringstream& write(_InputIterator first, _InputIterator last) {
    char* data = (char*)&(*first);
    auto n = std::distance(first, last);
    oss.write(data, n * sizeof(*first));
    return *this;
  }

  template <typename T, typename std::enable_if<std::is_fundamental<T>::value,
                                                bool>::type = true>
  bostringstream& write(const T* v, std::streamsize count) {
    oss.write((char*)v, sizeof(T) * count);
    return *this;
  }

  auto rdbuf() const { return oss.rdbuf(); }

  auto str() const { return oss.str(); }

  std::size_t size() { return oss.tellp(); }

protected:
  std::ostringstream oss;
};

例子:

#include <array>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include "bsstream.hpp"

int main(int argc, char **argv) {

  int i = 1;
  float j = 1.1;
  double k = 1.2;
  std::vector<int> ii{1,2};
  std::vector<double> jj{1.2,2.2};
  std::string kk = "abcd";
  std::array<int, 2> ll{3,4};
  int l[] = {1,2};

  bostringstream of;
  of << i << j <<k;
  of <<ii << jj << kk << ll;
  of.write(l, 2);

  std::ofstream oof("foo.bin", std::ios::binary);
  oof << of.str();
  oof.close();

}

不是一个优雅的解决方案,但有效且灵活

于 2021-09-06T01:53:08.140 回答