Search code examples
.netvisual-studiovisual-studio-2012visual-c++c++-cli

Saving text file in its entirety


I need help in coding the proper way to save a text file's contents. So far, I've managed to save and create a text file, but when I open it, it's always empty. I am using Visual Studio 2012 and creating a Windows form with c++-CLI.

This is what I have:

private: System::Void saveToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) 
          {
  IO::Stream^ myStream;
  SaveFileDialog^ saveFileDialog1 = gcnew SaveFileDialog;
  saveFileDialog1->Filter = "txt files (*.txt)|*.txt";
  saveFileDialog1->FilterIndex = 1;
  saveFileDialog1->RestoreDirectory = true;
  if ( saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
  {
     if ( (myStream = saveFileDialog1->OpenFile()) != nullptr )
     {

        // Code to write the stream goes here.
        myStream->Close();
     }
  }
}

Basically, I'm trying to achieve the code below, the only thing different is that I am using c++-CLI not c# and instead of writing some text in the file as (bw.Write("Example text file.") does, I would want the text file to save my text box values in my windows form, so something like bw.Write(txtTextBox1->Text). But as far as I'm concerned, c++ doesn't use those same commands c# does.

SaveFileDialog sgd = new SaveFileDialog();
sfd.Filter = "Text File|*.txt";
sfd.FileName = "My Text File";
sfd.Title = "Save Text File";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string path = sft.FileName;
    BinaryWriter bw = new BinaryWriter(File.Create(path));
    bw.Write("Example text file.");
    bw.Dispose();
}

Here are some images of my program. I've also included a picture of the print preview button. I'd basically like for my save button to save a text file with the contents that my print preview displays.

Windows Form
Print Preview


Solution

  • Why anyone would want to implement a Forms program in C++/CLI is beyond me. C++/CLI is wonderful for many things, but for something that is purely managed (like the Forms API), I think C# is better. There are more code examples to draw from, and the syntax is generally more concise and convenient.

    That said, here is a C++/CLI version of the C# code you included, with the small change that it uses StreamWriter instead of BinaryWriter:

    SaveFileDialog^ sfd = gcnew SaveFileDialog();
    
    try
    {
        sfd->Filter = "Text File|*.txt";
        sfd->FileName = "My Text File";
        sfd->Title = "Save Text File";
        if (sfd->ShowDialog() == DialogResult::OK)
        {
            String^ path = sfd->FileName;
            StreamWriter^ writer = gcnew StreamWriter(File::Create(path));
            try
            {
                writer->Write("Example text file.");
            }
            finally
            {
                delete writer;
            }
        }
    }
    finally
    {
        delete sfd;
    }
    

    Note that other than some minor syntax differences and the lack of C#'s using convenience idiom, it is basically identical to the C# code. You will see similar parallels for the rest of your implementation. C++/CLI uses (or at least, can use) exactly the same methods that are used in C#.