Save and load data in txt file

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
mxoliveira73
Experienced Solver
Experienced Solver
Posts: 54
Joined: Sun May 05, 2019 7:12 am

Save and load data in txt file

Post by mxoliveira73 » Mon Jul 22, 2019 4:11 pm

I'm need write/save and after load data in a txt file.
For example, I have 3 variables: int a = 3 - int b = 32 - int c = 25;
First I write/save in a txt file; after, in other moment, I open, read and load this variables to my application.

I'm looking for a simple example, tips or where can I search this topic, please....


I'm using this codes to save and open.


OPEN:

Code: Select all

 wxFileDialog
        openFileDialog(this, _("Open TXT file"), "", "",
                       "TXT files (*.txt)|*.txt", wxFD_OPEN|wxFD_FILE_MUST_EXIST);
    if (openFileDialog.ShowModal() == wxID_CANCEL)
        return;     // the user changed idea...

    wxFileInputStream input_stream(openFileDialog.GetPath());
Save:

Code: Select all

 wxFileDialog
        saveFileDialog(this, _("Salvar TXT file"), "", "",
                       "TXT files (*.txt)|*.txt", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
    if (saveFileDialog.ShowModal() == wxID_CANCEL)
        return;     // the user changed idea...
    wxFileOutputStream output_stream(saveFileDialog.GetPath());
Last edited by mxoliveira73 on Mon Jul 22, 2019 5:03 pm, edited 2 times in total.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2088
Joined: Sun Jan 03, 2010 5:45 pm

Re: Save and open data in txt file

Post by PB » Mon Jul 22, 2019 5:01 pm

If you need to save simple variables in a text file, I would recommend looking into wxFileConfig, see wxConfigBase for description.

My second choice would be wxTextFile, but you will need to do all the parsing by yourself, not that hard actually, particularly if you do not need sections.

mxoliveira73
Experienced Solver
Experienced Solver
Posts: 54
Joined: Sun May 05, 2019 7:12 am

Re: Save and load data in txt file

Post by mxoliveira73 » Mon Jul 22, 2019 5:10 pm

To save and load user settings, what do you recomend? I thought in save variables in a txt file and after load it. Is there a good idea? Or is there other way better? Directly: How is the better way to save and load user settings?
Last edited by mxoliveira73 on Mon Jul 22, 2019 5:31 pm, edited 1 time in total.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2088
Joined: Sun Jan 03, 2010 5:45 pm

Re: Save and load data in txt file

Post by PB » Mon Jul 22, 2019 5:31 pm

For storing application settings, IMO using wxConfigBase would be the best.

It uses the platform-preferred way to store the structured and typed settings (e.g., Registry on MSW), taking care of platform-specific details (such as where to store the settings in the Registry) as well.

mxoliveira73
Experienced Solver
Experienced Solver
Posts: 54
Joined: Sun May 05, 2019 7:12 am

Re: Save and load data in txt file

Post by mxoliveira73 » Mon Jul 22, 2019 5:38 pm

Big thanks, PB! One more time, big thanks for your help....

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2276
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: Save and load data in txt file

Post by evstevemd » Mon Jul 22, 2019 6:19 pm

For general purpose there is wxTextFile
https://wiki.wxwidgets.org/Reading_text_from_a_file
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

Post Reply