0
votes

Im trying to use this kind of iterators, but I have got errors for the following code:

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

The problems are:

1- ostreambuf_iterator<float> out_it (FILE);

error: invalid conversion from ‘void*’ to ‘std::ostreambuf_iterator::streambuf_type* {aka std::basic_streambuf >*}’ [-fpermissive]

2- FILE.read(reinterpret_cast<const char *>(&myWidth), sizeof(myWidth));

error: invalid conversion from ‘const char*’ to ‘std::basic_istream::char_type* {aka char*}’ [-fpermissive]

3- FILE.read(reinterpret_cast<const char *>(&myHeight), sizeof(myHeight));

error: invalid conversion from ‘const char*’ to ‘std::basic_istream::char_type* {aka char*}’ [-fpermissive]

4- std::ifstream FILE(Path, std::ios::in | std::ifstream::binary);

error: invalid conversion from ‘void*’ to ‘std::ostreambuf_iterator::streambuf_type* {aka std::basic_streambuf >*}’ [-fpermissive]

5- copy(iter.begin(), iter.end(), std::back_inserter(myVector));

error: ‘class std::istreambuf_iterator’ has no member named ‘begin’

error: ‘class std::istreambuf_iterator’ has no member named ‘end’

What am I doing wrong? I'm using the nsight eclipse from CUDA, but compiling a C++ project with g++. I refer to: Reading and writing a std::vector into a file correctly

#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <vector>

using namespace std;

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));
    ostreambuf_iterator<float> out_it (outFILE);
    copy(V.begin(), V.end(), out_it);
}

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<const char *>(&myWidth), sizeof(myWidth));
    inFILE.read(reinterpret_cast<const char *>(&myHeight), sizeof(myHeight));
    istreambuf_iterator<float> iter(inFILE);
    copy(iter.begin(), iter.end(), std::back_inserter(myVector));
}

int main() {

    Write_File_Matrix("M.dat");
    Read_File_Matrix("R.dat");

    return 0;
}
2
Please provide a SSCCE.Robᵩ
OK. I will put the class linearMatrix header and implementation.FacundoGFlores
The first S stands for short. Can you reproduce the problem in a 10-line program?Robᵩ
Thanks for supplying short stand-alone program. It really does help.Robᵩ
Thank you Rob too. You gave me a good answer and advice.FacundoGFlores

2 Answers

4
votes

EDIT: removed incorrect answer to 1, 4.

Re: 2, 3. You can't read into a const char*; it's const. Cast to a char* instead. That's what the error message says.

Re: 5. Yes, iterators don't have begin() and end() member functions. You've already got an iterator to the beginning of the file (once the previous problems are solved). Its name is iter. Now you need an end-of-file iterator, and the correct call is std::copy(iter, end_iter, std::back_inserter(myVector));.

4
votes

1- ostreambuf_iterator out_it (FILE);

error: invalid conversion from ‘void*’ to ‘std::ostreambuf_iterator::streambuf_type* {aka std::basic_streambuf >*}’ [-fpermissive]

std::ostreambuf_iterator takes as template parameter the character type of the stream, NOT the type you want to convert to. Try:

std::ostreambuf_iterator<char> out_it(FILE);

Ditto for #4.


EDIT: Let's solve your big problem instead of your small ones:

Don't bother with iterators, just write the vector data out all in one go:

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

Reading, however, still needs to be one-piece-at-a-time.

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