C++ 为什么我的程序在读取/写入文件时会删除最重要的数字?

C++ 为什么我的程序在读取/写入文件时会删除最重要的数字?,c++,file-io,C++,File Io,我编写了两个函数,将一个4维向量保存到一个文件中,然后将向量读回文件中 void saveLayer(string filename, int layer){ ofstream ofile(filename, ios::out | ios::trunc); vector<vector<vector<vector<double>>>> oweights(layers[layer]->weights); for(vecto

我编写了两个函数,将一个4维向量保存到一个文件中,然后将向量读回文件中

void saveLayer(string filename, int layer){
    ofstream ofile(filename, ios::out | ios::trunc);
    vector<vector<vector<vector<double>>>> oweights(layers[layer]->weights);
    for(vector<vector<vector<double>>> stratg:oweights){
        for(vector<vector<double>>layeri:stratg){
            for(vector<double> neuronq:layeri){
                for(double q:neuronq){
                    ofile<<setprecision(15)<<q;
                }
            }
        }
    }
    vector<vector<vector<double>>> obiases=layers[layer]->biases;
    for(vector<vector<double>>z:obiases){
        for(vector<double> w:z){
            for(double q:w){
                ofile<<setprecision(15)<<q;
            }
        }
    }
    ofile.close();
}

void loadLayer(string filename, int nS, vector<int> layerStruct){
    vector<vector<vector<vector<double>>>> newWeights;
    vector<vector<vector<double>>> newBiases;
    ifstream ifile(filename, ios::in);
    for(int i=0; i<nS; i++){
        vector<vector<vector<double>>> netWeights;
        for(int z=1; z<layerStruct.size(); z++){
            vector<vector<double>> layerWeights;
            for(int x=0; x<layerStruct[z]; x++){
                vector<double> neuronWeights;
                for(int y=0; y<layerStruct[z-1]; y++){
                    double w;
                    ifile>>w;
                    neuronWeights.push_back(w);
                }
                layerWeights.push_back(neuronWeights);
            }
            netWeights.push_back(layerWeights);
        }
        newWeights.push_back(netWeights);
    }
    for(int i=0; i<nS; i++){
        vector<vector<double>> netBiases;
        for(int z=1; z<layerStruct.size(); z++){
            vector<double> layerBiases;
            for(int x=0; x<layerStruct[z]; x++){
                double neuronBias;
                ifile>>neuronBias;
                layerBiases.push_back(neuronBias);
            }
            netBiases.push_back(layerBiases);
        }
        newBiases.push_back(netBiases);
    }
    layers[0]->numStrats=nS;
    layers[0]->weights=newWeights;
    layers[0]->biases=newBiases;
    ifile.close();
}
void保存层(字符串文件名,int层){
文件流(文件名,ios::out | ios::trunc);
向量权重(层[层]->权重);
用于(矢量策略:oweights){
对于(vectorlayeri:stratg){
对于(矢量神经元Q:layeri){
for(双q:q){

ofile写入输出文件的数据之间没有分隔符。给定1.234和5.678

ofile<<setprecision(15)<<q;
不知道一个数字应该在哪里结束,下一个数字应该在哪里开始。因此,它会一直读取,直到if找到一个不可能是
双精度
的字符

例如,1.2345.678将被读取到1.2345,在那里它会找到第二个
。浮点数不能有两位小数(即使这是
双精度的
),因此解析器停止,在流中留下.678供下次读取,并返回1.2345。下次读取大约.678并返回0.678

最重要的数字现在神秘地消失了,因为一个
double
只适用于15位数字,你可能不会注意到它被固定在前一个数字作为第16位数字

可能的解决方案:在每个数字后面写一个空格

ofile<<setprecision(15)<<q << ' ';

OFILUEUNRELATED
for(vector stratg:oweights)
正是为(vector stratg:oweights)制作的
auto
for(auto stratg:oweights)
要干净得多,是吗?请注意,尽管在这两种情况下都处理值,其中引用可能更合适。编译器很智能,因此它可能不会生成所需的所有副本,而是
用于(const auto&stratg:oweights)
确保了这一点。
write
read
?问题发生在哪里?是否将值
0.2345
写入文件?这意味着
read
操作做得很好。如果
1.2345
正在写入,但
read
没有返回正确的值,则
read
是问题。是哪一个?您是否使用了调试器来找出问题发生的时间?我不知道问题是否出在save或load函数上。您可能可以通过查看输出文件来缩小这种可能性。如果文件不正确,save很可能是罪魁祸首——假设代码的其他部分没有说谎it.user4581301感谢您提供的提示。我不知道如何在不使用我不确定是否有效的相同读取方法的情况下检查输出文件。这些文件看起来是100%文本。在文本文件读取器中打开它们,看看它们包含什么。问题可能会归结为写入值之间缺少空格。如果写入1.234,然后写入5.678到文件该文件将包含1.2345.678,该文件将读回为1.2345和0.678
ofile<<setprecision(15)<<q << ' ';