How to open and save files in visual c++ using StreamWriter
Save Files:
if(saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) {
StreamWriter ^ sw = gcnew StreamWriter(saveFileDialog1->FileName);
sw->WriteLine(myTextBox->Text);
sw->Close();
}
Open Files (Change textBox1 to whatever form you're using):
if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
textBox1->Clear();
StreamReader ^ sr = gcnew StreamReader(openFileDialog1->FileName);
textBox1->Text = sr->ReadToEnd();
this->label1->Text = openFileDialog1->FileName;
sr->Close();
}
I recommend putting the save script inside a new function so you can call it before opening a new file.
Written by Harry Gogonis
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Visual c
Authors
harrygogonis
5.931K
Related Tags
#visual c
#vc
#open
#save
#file
#streamwriter
#cpp
#programming
#visual
#visual studios
#visual studio
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#