为什么 ofstream 将我未包含的值写入我的文件?

Why is ofstream writing values to my file that I did not include?

我正在制作一个程序,该程序使用函数将模式保存到文件中,并且像往常一样,我使用 ofstream 来执行此操作。该图案是 4 行 16 列的网格。当我在将数据写入文件之前输出变量值时,所有值都显示为正确;然而,当去检查文件时,总会有两三个附加值甚至没有包含在代码中。这是为什么?

相关代码如下:

void Sampler_SynthFrame::OnSavePattern(wxCommandEvent& event)
{
    wxFileDialog saveFileDialog(this, _("Choose a pattern file to load..."), "", "",
                                "PTN files (*.ptn)|*.ptn", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return;

    ///load file and error handling
    wxFileOutputStream output_stream(saveFileDialog.GetPath());
    if (!output_stream.IsOk())
    {
        wxLogError("Cannot open file '%s'.", saveFileDialog.GetPath());
        return;
    }
    else{
        string szSave_pattern;
        wxString path = saveFileDialog.GetPath();
        szSave_pattern = path.ToStdString();
        ofstream writeptn(szSave_pattern,ios::binary);
        writeptn << colorn << '\n' << color1 << '\n' << color2 << '\n' << color3 << '\n' << color4 << '\n' << color5 << '\n' << color6 << '\n' <<
        color7 << '\n' << color8 << '\n' << color8 << '\n' << color9 << '\n' << color10 << '\n' << color11 << '\n' << color12 << '\n' << color13 << '\n' <<
        color14 << '\n' << color15 << '\n' << color16 << '\n' << color17 << '\n' << color18 << '\n' << color19 << '\n' << color20 << '\n' << color21 << '\n' <<
        color22 << '\n' << color23 << '\n' << color24 << '\n' << color25 << '\n' << color26 << '\n' << color27 << '\n' << color28 << '\n' << color29 << '\n' <<
        color30 << '\n' << color31 << '\n' << color32 << '\n' << color33 << '\n' << color34 << '\n' << color35 << '\n' << color36 << '\n' << color37 << '\n' <<
        color38 << '\n' << color39 << '\n' << color40 << '\n' << color41 << '\n' << color42 << '\n' << color43 << '\n' << color44 << '\n' << color45 << '\n' <<
        color46 << '\n' << color47 << '\n' << color48 << '\n' << color49 << '\n' << color50 << '\n' << color51 << '\n' << color52 << '\n' << color53 << '\n' <<
        color54 << '\n' << color55 << '\n' << color56 << '\n' << color57 << '\n' << color58 << '\n' << color59 << '\n' << color59 << '\n' << color60 << '\n' <<
        color61 << '\n' << color62 << '\n' << color63;
        writeptn.close();
    }
}

这是网格的快照,因此您可以更好地了解正在发生的事情:

导出模式后,它应该看起来像 0 和 1 交替出现,但我得到的是:

然后在文件末尾:

您将color8 和color59 写入文件两次。在发布问题之前,您可能需要检查一下。