C++中有二进制内存流吗

ttygqcqt  于 11个月前  发布在  其他
关注(0)|答案(7)|浏览(94)

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

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

字符串
ch的内存看起来像这样:0x 313233-字符1,2和3的ASCII代码。我正在寻找一种方法来写入二进制值本身。也就是说,我希望内存中有0x 010203。问题是我希望能够编写函数

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;


有什么想法吗?

watbbzwu

watbbzwu1#

要向流(包括stringstreams)读写二进制数据,请使用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
编辑:要使用插入和提取操作符(< >)使其透明地工作< and >,您最好创建一个执行正确操作的派生streambuf,并将其传递给您想要使用的任何流。

yk9xbfzb

yk9xbfzb2#

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

//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);

up9lanfz

up9lanfz3#

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

s << char(1) << char(2) << char(3);

字符串

yduiuuwa

yduiuuwa4#

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

#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;

lg40wkob

lg40wkob5#

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

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对象不安全:泄漏指针和填充
  • 输出是特定于平台的:填充、字节序、整数类型
jexiocij

jexiocij6#

我真的很喜欢韩洛的方法,并且已经验证了它工作得很好!如果将oss成员变量改为使用std::stringstream (vs. ostingstream),这个类也可以用于使用重载的流提取操作符进行提取,如下所示:

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类型。

ct3nt3jp

ct3nt3jp7#

#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();

}


不是一个优雅的解决方案,但工作和灵活
编辑:

  • 2023年11月12日:我在我的项目中使用类似的代码有一段时间了。我没有使用std::ostringstream作为类成员,而是将指向std::ostream类型的指针传递给类并进行编写部分。它通常工作正常。

相关问题