我试图使用这种迭代器,但是我有以下代码的错误:
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- ostreambuf_iterator<float> out_it (FILE);
错误:从'void *'无效转换为 'std :: ostreambuf_iterator :: streambuf_type * {aka std :: basic_streambuf&gt; *}' [-fpermissive]
2- FILE.read(reinterpret_cast<const char *>(&myWidth), sizeof(myWidth));
错误:从'const char *'无效转换为'std :: basic_istream :: char_type * {aka char *}'[-fpermissive]
3- FILE.read(reinterpret_cast<const char *>(&myHeight), sizeof(myHeight));
错误:从'const char *'无效转换为 'std :: basic_istream :: char_type * {aka char *}'[-fpermissive]
4- std::ifstream FILE(Path, std::ios::in | std::ifstream::binary);
错误:从'void *'无效转换为 'std :: ostreambuf_iterator :: streambuf_type * {aka std :: basic_streambuf&gt; *}' [-fpermissive]
5- copy(iter.begin(),iter.end(),std :: back_inserter(myVector));
错误:'class std :: istreambuf_iterator'没有名为的成员 “开始”
错误:'class std :: istreambuf_iterator'没有名为的成员 “结束”
我做错了什么?我正在使用来自CUDA的nsight eclipse,但是使用g ++编译C ++项目。我指的是: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;
}
答案 0 :(得分:4)
编辑:删除了对1,4的错误答案。
回复:2,3。你无法读入const char*
;它是 const 。转而使用char*
。这就是错误信息所说的内容。
Re:5。是的,迭代器没有begin()
和end()
成员函数。你已经有了一个文件开头的迭代器(一旦解决了以前的问题)。它的名字是iter
。现在您需要一个文件结束迭代器,正确的调用是std::copy(iter, end_iter, std::back_inserter(myVector));
。
答案 1 :(得分:4)
1- ostreambuf_iterator out_it(FILE);
错误:无效转换为'void *'到'std :: ostreambuf_iterator :: streambuf_type * {aka std :: basic_streambuf&gt; *}'[-fpermissive]
std::ostreambuf_iterator
将流的字符类型作为模板参数,而不是您要转换为的类型。尝试:
std::ostreambuf_iterator<char> out_it(FILE);
同样#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 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);
}