我正在创建一个程序,它使用一个函数将模式保存到文件中,而且像往常一样,我使用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的交替,但我得到这个:
并再次在文件的末尾:
答案 0 :(得分:2)
您将color8和color59两次写入文件。您可能想在发布问题之前检查一下。