C++ 正确读取二进制文件和使用流迭代器

C++ 正确读取二进制文件和使用流迭代器,c++,stl,compiler-errors,iterator,iostream,C++,Stl,Compiler Errors,Iterator,Iostream,我正在尝试使用这种迭代器,但以下代码出现错误: void linearMatrix::Write_File_Matrix(const char *Path) { ofstream FILE(Path, std::ios::out | std::ofstream::binary); FILE.write(reinterpret_cast<const char *>(&myWidth), sizeof(float)); FILE.write(reinter

我正在尝试使用这种迭代器,但以下代码出现错误:

void linearMatrix::Write_File_Matrix(const char *Path)
{
    ofstream FILE(Path, std::ios::out | std::ofstream::binary);
    FILE.write(reinterpret_cast<const char *>(&myWidth), sizeof(float));
    FILE.write(reinterpret_cast<const char *>(&myHeight), sizeof(float));
    ostreambuf_iterator<float> out_it (FILE);
    copy(myVector.begin(), myVector.end(), out_it);
}

void linearMatrix::Read_File_Matrix(const char *Path)
{
    ifstream FILE(Path, std::ios::in | std::ifstream::binary);

    if(!FILE)
    {
        cerr << "Cannot open the file" << endl;
        exit(1);
    }
    FILE.read(reinterpret_cast<const char *>(&myWidth), sizeof(myWidth));
    FILE.read(reinterpret_cast<const char *>(&myHeight), sizeof(myHeight));
    istreambuf_iterator<float> iter(FILE);
    copy(iter.begin(), iter.end(), std::back_inserter(myVector));
}

编辑:删除了对1、4的错误答案

回复:2,3。不能读入
常量字符*
;它是常数。转换为
字符*
。这就是错误消息所说的

回复:5。是的,迭代器没有
begin()
end()
成员函数。在文件的开头已经有了一个迭代器(一旦前面的问题得到解决)。它的名字是iter。现在需要一个文件结束迭代器,正确的调用是
std::copy(iter,end_iter,std::back_inserter(myVector))

1-ostreambuf_迭代器out_it(文件)

错误:从“void*”到“std::ostreambuf_迭代器::streambuf_type*{aka std::basic_streambuf>*}”[-fppermissive]的转换无效

std::ostreambuf_迭代器
将流的字符类型作为模板参数,而不是要转换为的类型。尝试:

std::ostreambuf_iterator<char> out_it(FILE);
std::ostreambuf\u迭代器输出它(文件);
#4也一样


编辑:让我们解决你的大问题,而不是小问题:

不必使用迭代器,只需一次性写出矢量数据即可:

void Write_File_Matrix(const char *Path)
{
    float myWidth = 0;
    float myHeight = 0;
    vector<float> v;
    v[0] = 1;
    v[1] = 2;
    v[2] = 3;
    ofstream outFILE(Path, ios::out | ofstream::binary);
    outFILE.write(reinterpret_cast<const char *>(&myWidth), sizeof(float));
    outFILE.write(reinterpret_cast<const char *>(&myHeight), sizeof(float));
    outFile.write(reinterpret_cast<const char *>(&v[0]), v.size()*sizeof(float));
}
void Write\u File\u矩阵(常量字符*路径)
{
float myWidth=0;
浮动高度=0;
向量v;
v[0]=1;
v[1]=2;
v[2]=3;
ofstream outFILE(路径,ios::out | ofstream::binary);
outFILE.write(reinterpret_cast(&myWidth),sizeof(float));
outFILE.write(重新解释cast(&myHeight),sizeof(float));
outFile.write(重新解释强制转换(&v[0]),v.size()*sizeof(float));
}
然而,阅读仍然需要一次一件地完成

void Read_File_Matrix(const char *Path)
{
    float myWidth = 0;
    float myHeight = 0;
    vector<float> v;

    ifstream inFILE(Path, ios::in | ifstream::binary);

    if(!inFILE)
    {
        cerr << "Cannot open the file" << endl;
        exit(1);
    }
    inFILE.read(reinterpret_cast<char *>(&myWidth), sizeof(myWidth));
    inFILE.read(reinterpret_cast<char *>(&myHeight), sizeof(myHeight));
    float f;
    while( inFILE.read(reinterpret_cast<char *>(&f), sizeof(f)))
      v.push_back(f);
}
void Read\u File\u矩阵(常量字符*路径)
{
float myWidth=0;
浮动高度=0;
向量v;
ifstream infle(路径,ios::in | ifstream::binary);
如果(!infle)
{

cerr请提供一个。好的。我将把类linearMatrix头和实现放在一起。缩写。你能在一个10行程序中重现这个问题吗?谢谢你提供了一个简短的独立程序。它真的很有帮助。也谢谢你Rob。你给了我一个很好的答案和建议。Re:1,4:See,#2。构造函数确实采用了
流de>object。问题是模板类型。非常感谢Rob的帮助。但是我的编译器给了我:错误:从'void*'到'std::ostreambuf_迭代器::streambuf_type*{aka std::basic_streambuf*}'[-fppermissive]的转换无效,可能是因为我的输入错误:try
v.size()
,而不是
v.size
。Rob我尝试了read,但编译器给了我错误,错误与上面相同Rob我更改了它:infle.read(reinterpret_cast(&myWidth),sizeof(float));infle.read(reinterpret_cast(&myHeight),sizeof(float));现在我试图解决while()语句您可以将向量调整为预期大小,然后将所有信息直接读取到vector.data()中
void Read_File_Matrix(const char *Path)
{
    float myWidth = 0;
    float myHeight = 0;
    vector<float> v;

    ifstream inFILE(Path, ios::in | ifstream::binary);

    if(!inFILE)
    {
        cerr << "Cannot open the file" << endl;
        exit(1);
    }
    inFILE.read(reinterpret_cast<char *>(&myWidth), sizeof(myWidth));
    inFILE.read(reinterpret_cast<char *>(&myHeight), sizeof(myHeight));
    float f;
    while( inFILE.read(reinterpret_cast<char *>(&f), sizeof(f)))
      v.push_back(f);
}