二进制输出的ostream_iterator [英] ostream_iterator for Binary Output

查看:203
本文介绍了二进制输出的ostream_iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用 ostream_iterator 流式传输到二进制文件。但是 ostream_iterator 使用 FormattedOuputFunction 因此它将写入ASCII而不是二进制:


std :: ostream_iterator 是一个单次传递OutputIterator,它将类型 T 的连续对象写入到它所在的 std :: basic_ostream 构造,使用运算符<


除了编写我自己的迭代器一个使用迭代器编写二进制文件的方法?



一个简单的例子,我要做的,但 copy 语句将ASCII写入我的二进制文件:

  ofstream foo(foo.txt,ios_base ::二进制); 
vector< int> bar = {13,42};

copy(bar.cbegin(),bar.cend(),ostream_iterator< decltype(bar):: value_type>(foo)


解决方案

它可以工作,但你必须明确使用 ostream_iterator< char>



示例(包括忽略不重要):

  int main(int argc,char ** argv){
std :: vector< int> arr;

std :: ofstream fd(foo.txt,std :: ios :: binary | std :: ios :: out);

for(int i = 0; i <256; i ++)arr.push_back(i);

std :: ostream_iterator< char> oi(fd);
std :: copy(arr.begin(),arr.end(),oi);
fd.close();
return 0;
}

将在foo.txt中写入0到255的256个字节。 p>




上面假设您想​​直接将int的值作为char写入文件。从您的注释,您想要以本地主机端字节中的4字节值(假设int32_t)写入int值。我将使用辅助类来完成这项工作:

  class bint {
private:
char c [sizeof(int)];

public:
bint(const int i){//允许bint b = 5;
:: memcpy(c,& i,sizeof(c));
}
operator int()const {//允许:bint b = 5; int i = b =>给出i = 5
int i;
:: memcpy(& i,c,sizeof(int));
return i;
}
const char * getBytes()const {//给予公共只读访问字节
return c;
}
};

std :: ostream& operator<<<(std :: ostream& out,const bint& b){
out.write(b.getBytes(),sizeof(int));
return out;
}

int main(int argc,char ** argv){
std :: vector< int> arr;

std :: ofstream fd(foo.txt,std :: ios :: binary | std :: ios :: out);

for(int i = 0; i <256; i ++)arr.push_back(i);

std :: ostream_iterator< bint> oi(fd);
std :: copy(arr.begin(),arr.end(),oi);
fd.close();
return 0;
}

这一个写入1024字节(大小为4的整数) 256个第一整数。它会自动适应其他大小的int。


I want to be able to use an ostream_iterator to stream to a binary file. But the ostream_iterator uses a FormattedOuputFunction so it will write ASCII, not binary:

std::ostream_iterator is a single-pass OutputIterator that writes successive objects of type T into the std::basic_ostream object for which it was constructed, using operator<<

Beyond writing my own iterator is there a way to use an iterator to write binary?

A simplified example of what I'm trying to do, but the copy statement is going to write ASCII to my binary file:

ofstream foo("foo.txt", ios_base::binary);
vector<int> bar = {13, 42};

copy(bar.cbegin(), bar.cend(), ostream_iterator<decltype(bar)::value_type>(foo));

解决方案

It works, but you will have to explicitely use an ostream_iterator<char>.

Example (includes omitted for brievety):

int main(int argc, char **argv) {
    std::vector<int> arr;

    std::ofstream fd("foo.txt", std::ios::binary | std::ios::out);

    for (int i=0; i<256; i++) arr.push_back(i);

    std::ostream_iterator<char> oi(fd);
    std::copy(arr.begin(), arr.end(), oi);
    fd.close();
    return 0;
}

will write the 256 bytes for 0 to 255 in foo.txt.


Above assumed that you wanted to write directly the value of the int as a char to the file. From your comment, you want to write the int value as a 4 bytes value (assuming int32_t) in native host endianness. I would use an auxilliary class to do the job:

class bint {
private:
    char c[sizeof(int)];

public:
    bint(const int i) { // allows bint b = 5;
        ::memcpy(c, &i, sizeof(c));
    }
    operator int() const {  // allows : bint b = 5; int i=b => gives i=5
        int i;
        ::memcpy(&i, c, sizeof(int));
        return i;
    }
    const char *getBytes() const { // gives public read-only access to the bytes
        return c;
    }
};

std::ostream& operator <<(std::ostream& out, const bint& b) {
    out.write(b.getBytes(), sizeof(int));
    return out;
}

int main(int argc, char **argv) {
    std::vector<int> arr;

    std::ofstream fd("foo.txt", std::ios::binary | std::ios::out);

    for (int i=0; i<256; i++) arr.push_back(i);

    std::ostream_iterator<bint> oi(fd);
    std::copy(arr.begin(), arr.end(), oi);
    fd.close();
    return 0;
}

This one writes 1024 bytes (for integer of size 4) containing the representations of the 256 first integers. It would automatically adapts to other sizes of int.

这篇关于二进制输出的ostream_iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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