作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 C++ 写了一个小文件处理程序类
它有一个管理 RAII 文件关闭的基类和两个派生类,一个用于写入,一个用于读取。派生类有 2 个方法写入或读取值和写入或读取 vector 。
#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);
}
}
};
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;
}
wrote a value: 7 offset: 12
wrote a value: 1 offset: 20
wrote a value: 2 offset: 28
wrote a value: 3 offset: 36
wrote a value: 4 offset: 44
wrote a value: 6 offset: 52
wrote a value: 10 offset: 60
wrote a value: 15 offset: 68 <--- tells me he wrote 15
value read: 12 offset: 8
12
value read: 7 offset: 12
size of vector: 7
value read: 1 offset: 20
value read: 2 offset: 28
value read: 3 offset: 36
value read: 4 offset: 44
value read: 6 offset: 52
value read: 10 offset: 60
value read: 10 offset: 68 <--- problem
vector size: 7
1 2 3 4 6 10 10 last val: 10 <--- last value should be 15
Hello world!
最佳答案
Is my file handler solution good?
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
ofile file
实例将持续到
main
结束.
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
value read: 10 current offset: 72
vector size: 7
1 2 3 4 6 10 10 last val: 10
ofile
的析构函数中关闭,到目前为止可能存在内部缓冲(即未刷新)的内容,尽管流会将它们报告为已写入。
ofile file
的范围|必须受到限制。
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;
}
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
value read: 15 current offset: 72
vector size: 7
1 2 3 4 6 10 15 last val: 15
I don't understand why it writes or read the last value 2 times?
iFile::read_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;
}
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
AARG! Input failed. :-(
value read: 10 current offset: 72
vector size: 7
1 2 3 4 6 10 10 last val: 10
关于c++ - 二进制文件处理程序未写入最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452504/
我是一名优秀的程序员,十分优秀!