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