I need to output text from a textBox into a text file selected in a Save File Dialog. My code is below. I also need to write out the address of the file.
private: System::Void saveToolStripMenuItem1_Click(System::Object^ sender,
System::EventArgs^ e)
{
SaveFileDialog saveFile;
ofstream ofs;
saveFile.ShowDialog(); //returns a DialogResult value (DialogResult.OK)
string str = marshal_as<string>(saveFile.FileName); //file name
ofs.open(str);
if (str != "") //if a file is selected
{
ofs << "TestMessage 1, 2" << endl;
ofs << str; //Displays current file name
ofs << conc->GetConc(str, txtDisplay) << endl;
ofs << marshal_as<string>(conc->GetConc(str, txtDisplay)) << endl;
ofs.close();
}
else
MessageBox::Show("No file is selected for output.");
}
So far I think what I've got is working. However I still need to be able to get output from the textbox. I've tried using something like the following, but it's not working. Note: this would go just before "ofs.close();"
ofs << txtDisplay->ToString() << endl;
I'm thinking the problem is with my GetConc method. Below are the header and body for the method. Am I returning the wrong thing?
string GetConc(string stringEntry, TextBox ^ tb);//HEADER
string Concordance::GetConc(string stringEntry, TextBox ^ tb)//BODY
{
string s = stringEntry + " ";
String ^s2 = gcnew String(s.c_str());
// add stringEntry to display string + " "
// for
// if found, add iter->second
int count = 0;
map <string, vector<int>>::iterator iter;
for (auto iter = m.cbegin(); iter != m.cend(); iter++)
{
string test = iter->first;
if (test == stringEntry)
{
vector<int>::iterator i;
for (auto i = iter->second.cbegin(); i != iter->second.cend(); i++)
{
s2 += *i;
if (i + 1 != iter->second.cend())
s2 += ",";
}
}
}
tb->Text += s2 + "\r" + "\n";
return s;
}