Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void OnFileSave(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Title = "Save a Binary File";
    saveFileDialog1.Filter = "Binary Files (*.log)|*.log|All files (*.*)|*.*";
    saveFileDialog1.DefaultExt = ".log";
    saveFileDialog1.FilterIndex = 1;

    string str;
    double flt;

    DialogResult dr;
    dr = saveFileDialog1.ShowDialog();

    if (dr == DialogResult.OK)
    {
        string savefile = saveFileDialog1.FileName;

        try
        {
             FileStream fs = new FileStream(savefile, FileMode.OpenOrCreate);
             BinaryWriter bw = new BinaryWriter(fs);
            // Write data
        }
        catch
        {
            MessageBox.Show("File save error.");
        }
    }
}


What I have tried:

I have several data grid data structures that I am using. The data is written to structures comprised of strings and doubles. Able to save the initial file OK which most of the structures are not populated. Then I add some additional data into my structures and try and save again which generates the error that it fails to open. In debug when it encounters this line it fails to open:

FileStream fs = new FileStream(savefile, FileMode.OpenOrCreate);

In my reading this lie above is suppose to create a new file is one does not exits or replace the file. The first case works. The second case files.

Writing in C++ using windows forms with Visual Studio 10
Posted
Updated 11-Dec-20 20:10pm
v3
Comments
Rick York 11-Dec-20 17:50pm    
It appears to me you are writing in C#.

FileMode.OpenOrCreate doesn't "replace", it "opens" the file for subsequent (file) operations.

What happens next depends on your (next) file operations; which may not be compatible with the "mode". (like positioning).

The "create" part (of mode) is so you don't have to when you need a file and there is none.

FileMode.OpenorCreate replaces the contents of the exsiting file[^]
 
Share this answer
 
See the code posted with this answer [^].
Quote:
The documentation is correct – but note that open and append are not synonymous. FileMode.OpenOrCreate is not overwriting the file, but the stream does start at the beginning of the file if one already exists. What you are observing is the contents being overwritten by the StreamWriter, not the FileStream constructor overwriting the file.

You have to move the stream position to the end of the file to add text at the end. To do this, you could move the position with FileStream.Seek() or change the FileMode to FileMode.Append. However, using FileMode.Append also requires making the stream write-only, rather than read-write.
.If you intend to always delete the existing file:
if (File.Exists(savefile))
{
    File.Delete(savefile);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900