can't make use of string data type Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Emerald2240
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue Dec 11, 2018 1:38 pm

can't make use of string data type

Post by Emerald2240 » Wed Jan 09, 2019 9:06 pm

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
Last edited by doublemax on Wed Jan 09, 2019 9:47 pm, edited 1 time in total.
Reason: Added code tags

Kvaz1r
Earned some good credits
Earned some good credits
Posts: 136
Joined: Tue Jun 07, 2016 1:07 pm

Re: can't make use of string data type

Post by Kvaz1r » Thu Jan 10, 2019 9:31 am

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
.

Post Reply