C++ 二进制文件处理程序未写入最后一个值
我用C编写了一个小文件处理程序类++ 它有一个管理RAII文件关闭的基类和两个派生类,一个用于写入,一个用于读取。派生类有两个方法:写入或读取值和写入或读取向量C++ 二进制文件处理程序未写入最后一个值,c++,binaryfiles,read-write,C++,Binaryfiles,Read Write,我用C编写了一个小文件处理程序类++ 它有一个管理RAII文件关闭的基类和两个派生类,一个用于写入,一个用于读取。派生类有两个方法:写入或读取值和写入或读取向量 #include <vector> #include <fstream> #include <iostream> #include <algorithm> class File{ protected: std::streampos offset; std::fstrea
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
class File{
protected:
std::streampos offset;
std::fstream file;
public:
File(){
offset = 0;
}
virtual ~File(){
file.close();
}
};
class oFile : public File{
public:
oFile(std::string const& filename) {
file.open(filename, std::ios::out|std::ios::binary);
}
template <typename T>
void write_value(T value){
file.seekp(offset);
file.write(reinterpret_cast<char*> (&value), sizeof(T));
offset += sizeof(T);
std::cout<< "wrote a value: " << value << " current offset: " << offset << std::endl;
}
template <typename T>
void write_vector(std::vector<T> v){
write_value(v.size());
std::for_each(v.begin(), v.end(), [this](T& i){write_value(i);});
}
};
class iFile : public File{
public:
iFile(std::string const& filename){
file.open(filename, std::ios::in|std::ios::binary);
}
template <typename T>
void read_value(T& v){
file.seekg(offset);
file.read(reinterpret_cast<char*> (&v), sizeof(T));
offset += sizeof(T);
std::cout << "value read: " << v << " current offset: " << offset << std::endl;
}
template <typename T>
void read_vector(std::vector<T>& v){
typename std::vector<T>::size_type vsize;
read_value(vsize);
std::cout << "size of vector: " << vsize << std::endl;
for(typename std::vector<T>::size_type i = 0; i < vsize; ++i){
T value;
read_value(value);
v.push_back(value);
}
}
};
#包括
#包括
#包括
#包括
类文件{
受保护的:
std::streampos偏移量;
std::fstream文件;
公众:
文件(){
偏移量=0;
}
虚拟文件(){
file.close();
}
};
文件的类:公共文件{
公众:
oFile(标准::字符串常量和文件名){
打开(文件名,std::ios::out | std::ios::binary);
}
模板
无效写入值(T值){
文件。seekp(偏移量);
write(reinterpret_cast(&value),sizeof(T));
偏移量+=尺寸f(T);
标准::cout
我的文件处理程序解决方案好吗
对于资源管理来说,这是一种很好的推荐方式
然而,对于RAII,变量的范围(以及寿命)应该更加仔细地考虑
在OP的公开代码中:
int main()
{
oFile file("test.ndf");
double b = 12.;
file.write_value(b);
std::vector<double> v{1., 2., 3., 4., 6., 10., 15.};
file.write_vector(v);
iFile ifile("test.ndf");
double ib;
ifile.read_value(ib);
std::cout << ib << std::endl;
std::vector<double> iv;
ifile.read_vector(iv);
std::cout << "vector size: " << iv.size() << std::endl;
for(auto val : iv){
std::cout << val << " ";
}
std::cout << "last val: " << iv[iv.size() - 1] << std::endl;
} // <-- all local variables incl. file will be destroyed here
(我在这里转载了《OP》一期)
由于内部流在文件
的析构函数中被关闭,因此到目前为止,可能存在内部缓冲(即未刷新)的内容,尽管流将以书面形式报告这些内容
解决方案很简单:必须限制文件的范围。
固定代码:
int main()
{
std::vector<double> v{1., 2., 3., 4., 6., 10., 15.};
{ // start new scope
oFile file("test.ndf");
double b = 12.;
file.write_value(b);
file.write_vector(v);
} // close scope -> destroy file (and b)
iFile ifile("test.ndf");
double ib;
ifile.read_value(ib);
std::cout << ib << std::endl;
std::vector<double> iv;
ifile.read_vector(iv);
std::cout << "vector size: " << iv.size() << std::endl;
for(auto val : iv){
std::cout << val << " ";
}
std::cout << "last val: " << iv[iv.size() - 1] << std::endl;
}
我不明白为什么它会写入或读取最后一个值2次 直到我意识到我把太多精力放在读最后一个值上2次,我才明白这一点 实际上,它没有–它只是无法读取最后一个值,并再次错误地报告上一个值 为了验证这一点,我在
iFile::read\u value
中添加了一个最小的“错误处理”:
template <typename T>
void read_value(T& v){
file.seekg(offset);
file.read(reinterpret_cast<char*> (&v), sizeof(T));
if (!file) std::cerr << "AARG! Input failed. :-(\n";
offset += sizeof(T);
std::cout << "value read: " << v << " current offset: " << offset << std::endl;
}
当然,文件I/O可能会因各种原因而失败。因此,文件操作的成功应始终进行检查。谢谢!我像你一样疯狂地专注于读取最后一个值。事实上,如果文件在打开时失败,我会检查文件,我想不到读取后会失败。谢谢你的额外帮助继续解释范围!@Pella86打开文件只是一个可能失败的操作,但读/写也可能失败,因此,也应该检查是否成功。假设文件I/O经过超级校对,这仍然是正确的。可能有超出您能力范围的原因-例如,在您完成此操作之前,磁盘卷已超过文件,或者只是一个愚蠢的用户在同步之前拔出了U盘…;-)@Pella86实际上,在std::streams中依赖RAII也有点危险。流应该明确地关闭(或刷新)(在销毁之前)以注意错误(例如,在刷新最后的缓冲内容时)。销毁之后,这就不可能了。流状态是遥不可及的(当然),即使为流启用了异常,它们也会在流析构函数中被静默捕获,因为析构函数永远不会抛出。
int main()
{
std::vector<double> v{1., 2., 3., 4., 6., 10., 15.};
{ // start new scope
oFile file("test.ndf");
double b = 12.;
file.write_value(b);
file.write_vector(v);
} // close scope -> destroy file (and b)
iFile ifile("test.ndf");
double ib;
ifile.read_value(ib);
std::cout << ib << std::endl;
std::vector<double> iv;
ifile.read_vector(iv);
std::cout << "vector size: " << iv.size() << std::endl;
for(auto val : iv){
std::cout << val << " ";
}
std::cout << "last val: " << iv[iv.size() - 1] << std::endl;
}
template <typename T>
void read_value(T& v){
file.seekg(offset);
file.read(reinterpret_cast<char*> (&v), sizeof(T));
if (!file) std::cerr << "AARG! Input failed. :-(\n";
offset += sizeof(T);
std::cout << "value read: " << v << " current offset: " << offset << std::endl;
}