Tutorial 6 : File Saving, Opening and Using Common Dialog Boxes Using Visual C++.NET 2010, 2008
This program will show you how to save to file, open from file and how to use dialog boxes to perform these tasks. Click here for the source code to the File Saving, Opening Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Begin by placing a text file call "myfile.txt" in your inner-most debug directory with a few lines of text placed in it of your choosing. Make this file in Visual Studio by going to >File>New>File>General>Text File. Place this file in the directory with the source (.cpp) you are creating. Then design a form which looks as follow with a listbox to the left and a textBox to the right with multiline property selected. Add a openFileDialog and a saveFileDialog tool to your form. To the right of the listbox is a textbox with its multiline property selected.
Add to the namespace region:
using namespace System::IO;
Add the following code to the relevant buttons:
This program will show you how to save to file, open from file and how to use dialog boxes to perform these tasks. Click here for the source code to the File Saving, Opening Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Begin by placing a text file call "myfile.txt" in your inner-most debug directory with a few lines of text placed in it of your choosing. Make this file in Visual Studio by going to >File>New>File>General>Text File. Place this file in the directory with the source (.cpp) you are creating. Then design a form which looks as follow with a listbox to the left and a textBox to the right with multiline property selected. Add a openFileDialog and a saveFileDialog tool to your form. To the right of the listbox is a textbox with its multiline property selected.
using namespace System::IO;
Add the following code to the relevant buttons:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
FileStream ^fileInput = gcnew FileStream("myfile.txt", FileMode::OpenOrCreate, FileAccess::ReadWrite);
StreamReader ^streamIn = gcnew StreamReader(fileInput);
while(!streamIn->EndOfStream)
{
listBox1->Items->Add(streamIn->ReadLine());
};
streamIn->Close();
}
catch(IOException ^)
{
textBox1->Text="something went wrong";
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
FileStream ^outFile = gcnew FileStream("mynewfile.txt", FileMode::Create, FileAccess::Write);
StreamWriter ^streamOut = gcnew StreamWriter(outFile);
streamOut->WriteLine(textBox1->Text);
streamOut->Close();
}
catch(IOException ^)
{
textBox1->Text="something went wrong";
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^myfile = "";
openFileDialog1->ShowDialog() ;
myfile = openFileDialog1->FileName;
try
{
FileStream ^fileInput = gcnew FileStream(myfile, FileMode::OpenOrCreate, FileAccess::ReadWrite);
StreamReader ^streamIn = gcnew StreamReader(fileInput);
while(!streamIn->EndOfStream)
{
listBox1->Items->Add(streamIn->ReadLine());
};
streamIn->Close();
}
catch(IOException ^)
{
textBox1->Text="something went wrong";
}
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^myfile = "";
saveFileDialog1->ShowDialog() ;
myfile = saveFileDialog1->FileName;
try
{
FileStream ^outFile = gcnew FileStream(myfile, FileMode::Create, FileAccess::Write);
StreamWriter ^streamOut = gcnew StreamWriter(outFile);
streamOut->WriteLine(textBox1->Text);
streamOut->Close();
}
catch(IOException ^)
{
textBox1->Text="something went wrong";
}
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
{
try
{
FileStream ^outFile = gcnew FileStream("myfile.txt", FileMode::Append, FileAccess::Write);
StreamWriter ^streamOut = gcnew StreamWriter(outFile);
streamOut->WriteLine(textBox1->Text);
streamOut->Close();
}
catch(IOException ^)
{
textBox1->Text="something went wrong";
}
}
|
0 comments:
Post a Comment