0

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;
}
5
  • 1
    Please provide a SSCCE. Commented Sep 12, 2012 at 14:08
  • OK. I will put the class linearMatrix header and implementation. Commented Sep 12, 2012 at 14:09
  • 1
    The first S stands for short. Can you reproduce the problem in a 10-line program? Commented Sep 12, 2012 at 14:15
  • 1
    Thanks for supplying short stand-alone program. It really does help. Commented Sep 12, 2012 at 15:04
  • Thank you Rob too. You gave me a good answer and advice. Commented Sep 12, 2012 at 15:05

2 Answers 2

4

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much Rob for your help. But my compilers gives me: error: invalid conversion from ‘void*’ to ‘std::ostreambuf_iterator<float>::streambuf_type* {aka std::basic_streambuf<float, std::char_traits<float> >*}’ [-fpermissive] for the outFile.write vector :S
That may be due to my typo: try v.size(), not v.size.
Rob I tried with read but the compiler gave me errors and they are the same as above
Rob i changed it:inFILE.read(reinterpret_cast<char *>(&myWidth), sizeof(float));inFILE.read(reinterpret_cast<char *>(&myHeight), sizeof(float)); Now Im trying to solve the while() sentence
you can resize vector to intended size and then read all information right into vector.data()
4

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

1 Comment

Re: 1,4: See en.cppreference.com/w/cpp/iterator/ostreambuf_iterator/… , #2. The constructor does take a stream object. The problem is the template type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.