二进制文件处理程序不写最后一个值

问题描述 投票:0回答:1

我用C++写了一个小文件处理类

它有一个管理RAII文件关闭的基类和两个派生类,一个用于写,一个用于读。衍生类有2个方法,分别是写或读值和写或读向量。

#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!

输出的最后一行是向量内容,虽然在写法中告诉我写了正确的值(15),但最后的值是重复的。

我不明白为什么它要写或读最后一个值2次?我的文件处理程序解决方案好吗?有更好的方法吗?

使用 MinGW (v9.2.0) 用 Code::Blocks (v20.03) 编译。

c++ binaryfiles read-write
1个回答
1
投票

我的文件处理方案好吗?

RAII 资源管理是一个很好的、值得推荐的方法。

然而,对于RAII来说,变量的范围(以及生命周期)应该被更加仔细地考虑。

在OP.NET的暴露代码中,变量的范围(以及因此而产生的寿命)应该更加仔细地考虑。

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

现场演示在coliru上 (我在这里转载了OP的问题)。

由于内部流在分解器的 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

现场演示在coliru上


我不明白为什么它要写或读最后一个值2次?

这一点我也不清楚,直到我意识到我把太多的注意力集中在了 读取最后一次数值2次.

事实上,它不 - 只是未能读取最后一个值,并再次错误地报告前一个值。.

为了验证这一点,我在这里添加了一个最小的 "错误处理"。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

在coliru上进行现场演示

当然,文件IO是可能因为各种原因而失败的。所以,应随时检查文件操作的成功率。

© www.soinside.com 2019 - 2024. All rights reserved.