Page 1 of 1

can't make use of string data type

Posted: Wed Jan 09, 2019 9:06 pm
by Emerald2240
Hi guys! I recently embarked on a project to create a simple jotter app. I use codeblocks. I planned on using C++'s basic txt file save due to the fact i'm working with texts bu i came across a problem; I was easily able to create a text editor that stored values or variables using save slots for saving and load slots for displaying the results on the screen...
all my code seemed okay but for some reason it was incapable of accepting string data types, i experimented using both int and double data types and they worked perfectly. So my question is how do i make a string data type usable in my program?
Here's some code
1] Save slot

Code: Select all

 void projectcalciFrame::OnButton11Click(wxCommandEvent& event)
{
   string x;
   TextCtrl1->GetValue().ToStdstring((&x));
   Ofstream myfile("widgets.txt");
   myfile<<x;
   myfile.close();
}
2] Load slot

Code: Select all

void projectcalciFrame::OnButton11Click(wxCommandEvent& event)
{
   ifstream myfile ("widgets.txt");
   string x;
   while(myfile>>x){
      wxstring res = wxT(" ")
      res<<x;
      TextCtrl1->SetValue(res);
   }
}.
Please forgive my Crude Naivety, Typos,... etc

Re: can't make use of string data type

Posted: Thu Jan 10, 2019 9:31 am
by Kvaz1r
Convert from wxString to std::string:
Standard std::string using wxString::ToStdString(). The contents of the returned string use the current locale encoding, so this conversion is potentially destructive as well.
Btw wxWidgets provides classes and methods for writing and reading files so you can use them instead

Code: Select all

std::ofsream/std::ifstream
.