Search code examples
c++wxwidgetsofstream

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


I'm making a program that uses a function to save a pattern to a file, and as usual I'm using an ofstream to do so. The pattern is a grid of 4 rows and 16 columns. All of the values show up as correct when I output the variables' values right before it writes the data to a file; however, when go and check the file, there are always two or three added values that weren't even included in the code. Why is that?

Here is the relevant code:

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();
    }
}

Here is a snapshot of the grid, so you could get a better idea of what is going on:

enter image description here

After I export the pattern it should just look like 0's and 1's alternating, but instead I get this:

enter image description here

and again at the end of the file:

enter image description here


Solution

  • You write the color8 and color59 two times to the file. You might want to check that before posting the question.