Opening and Saving 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.
mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Opening and Saving

Post by mrmarky2 » Tue Apr 22, 2008 11:03 am

Hi,

I have nearly finished my program, I just need to get it to open and save files.

I have been looking at the Cross Platform GUI programming book and I found some code that might help:

For Opening:

Code: Select all

#include “wx/textfile.h”

void FilePrepend(const wxString& filename, const wxString& text)
{
  wxTextFile file;
  if (file.Open(filename))
  {
  size_t i;
  for (i = 0; i < file.GetLineCount(); i++)
    {
    file[i] = text + file[i];
    }
  file.Write(filename);
  }
}
I was hoping to write it so that the user can click on the open button, a dialog comes up like any normal open button, and they select which file they want to open. The only type of file they can open is a text file.

I think this is the kind of code I need, but Im not sure how to use it. All I want to do is to copy all the information from a file into an array. The files should be 9 rows and 9 columns each. So I would like a 2d array that stores each of the information.

Does anyone know how to use this?


And when saving I need a function that writes a 2d array to a file.

Anyone have any ideas?

Thanks

Mark

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Tue Apr 22, 2008 11:26 am

From the dialog tutorial (http://www.zetcode.com/tutorials/wxwidg ... l/dialogs/)
Sounds like you do something like this:

Code: Select all

    wxFileDialog * openFileDialog = new wxFileDialog(this);
    if (openFileDialog->ShowModal() == wxID_OK){

    }
Now I need it to get the information from it, and store it into a string.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Tue Apr 22, 2008 11:30 am


mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Tue Apr 22, 2008 12:07 pm

Thanks

I have looked at that :( I can't work out how to do it still.
In Sof's programming guide he used this

Code: Select all

WxMemo1->LoadFile(WxOpenFileDialog1->GetPath());
It loads a file to the memo. But it wont let me use LoadFile function in my program. :(

Mark :)

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Tue Apr 22, 2008 5:33 pm

mrmarky2 wrote:it wont let me use LoadFile function in my program. :(
please explain what you mean (i.e. post error messages, and code you tried) saying "it doesn't work" is never enough on a programming board

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Wed Apr 23, 2008 2:41 pm

Right

Heres the code I tried to use:

Code: Select all

wxString hold = LoadFile(WxOpenFileDialog1->GetPath());
And the error message, 'LoadFile' undeclared, first use this function.

Deschamps
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue May 01, 2007 12:16 pm

Post by Deschamps » Wed Apr 23, 2008 3:35 pm

In my humble opinion, you should study the whole source code provided in those guides and read the wxWidgets documentation, instead of copy/pasting pieces of code from here and there, expecting that it'll work magically.

In regards to that error, you should implement that function (or include the proper library, if that is the case). The compiler tells you that the related function doesn't exist in your code.

Regards.
Misquotations are the only quotations that are never misquoted

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Wed Apr 23, 2008 3:40 pm

I have tried including the library, it seems I havent found the right one, the reason why I am asking on the forum is because I have looked through the documentation, and I cant seem to get it working.

What I really started this post for was to ask how anyone here, would take all the information from a file into a string.

Can anyone tell me how they would do this? (With a little bit of code.)

Thanks for the comment

Mark

mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post by mc2r » Wed Apr 23, 2008 4:20 pm

I'm probably going to irritate you with this as most people on forums don't seem to be able to take criticism. So, don't take offense but do take the advice.

I think you need to seriously brush up on your c++ skills. The are a prerequisite for wxWidgets programming (with the exception of something like wxPython).

One of the main reasons I say this is you made the leap from example code calling a member function named LoadFile()

Code: Select all

WxMemo1->LoadFile(WxOpenFileDialog1->GetPath());
to thinking you could call it as a function.

Code: Select all

wxString hold = LoadFile(WxOpenFileDialog1->GetPath());
myClass->Something() and Something() are not the same thing, this is c++ 101. Sorry, not trying to be a dick but it's true and there is some serious conceptual misunderstandings that you will have to overcome to effectively use c++ or a library built around c++.

Other than learning c++ (for real), I would suggest looking at wxTextInputStream and wxTextOutputStream.

Both are in the docs and have samples of how to read or write (respectivly) a value. wxTextInputStream shows you how to open the stream from a file, It will be the same for wxTextOutputStream. Make yourself a nested loop

Code: Select all

for(row = .....){
    for(col = ....){
        //Read or write a value from or to a file
    }
}
-Max
Last edited by mc2r on Wed Apr 23, 2008 5:45 pm, edited 1 time in total.

Deschamps
Knows some wx things
Knows some wx things
Posts: 30
Joined: Tue May 01, 2007 12:16 pm

Post by Deschamps » Wed Apr 23, 2008 5:44 pm

mrmarky2 wrote:Can anyone tell me how they would do this? (With a little bit of code.)
.. and in regards to the wxFileDialog that Auria suggested a few messages ago, you could also consider reading the Using_common_dialogs from the wxWidgets wiki.

Regards.
Misquotations are the only quotations that are never misquoted

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Wed Apr 23, 2008 6:08 pm

I'm probably going to irritate you with this as most people on forums don't seem to be able to take criticism. So, don't take offense but do take the advice.

I think you need to seriously brush up on your c++ skills. The are a prerequisite for wxWidgets programming (with the exception of something like wxPython).
Thanks for the comment 8), I know I do need to brush up on my skills, I started this project and realised how little I knew half way through. I didn't want to stop part way through, so I was going to finish it, then come back and improve it later.

I only started c++ about 5-6 weeks ago, and started this project because I thought it was gonna be simple. How wrong I was :) .
.. and in regards to the wxFileDialog that Auria suggested a few messages ago, you could also consider reading the Using_common_dialogs from the wxWidgets wiki.
Thanks, i'll have a look at that now, :)

Mark

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Wed Apr 23, 2008 6:42 pm

I have had a look at both of those links,

Code: Select all

void BasicFrame::OnOpenFile( wxCommandEvent & event )
{
	wxFileDialog* openFileDialog =
		new wxFileDialog( this, _("Open file"), "", "", FILETYPES,
		                  wxOPEN, wxDefaultPosition);
 
	if ( openFileDialog->ShowModal() == wxID_OK )
	{
		SetCurrentFilename(openFileDialog->GetFilename());
		theText->LoadFile(openFileDialog->GetFilename());
		SetStatusText(GetCurrentFilename(), 0);
		SetStatusText(openFileDialog->GetDirectory(),1);
	}
}
http://wiki.wxwidgets.org/Beech:Using_common_dialogs This didn't help, theText was not declared, I have no idea what it is (ie string...), and the compiler complains that it han't been declared. The compiler doesn't like the SetCurrentFilename and SetStatusText either, it says they havent been declared either. I tried adding the include file for file dialogs, and it the compiler still says the same thing.
#include <wx/filedlg.h>

When I looked at wxTextInputStream http://docs.wxwidgets.org/2.8.6/wx_wxte ... readstring
I found this code as an example:

Code: Select all

wxFileInputStream input( "mytext.txt" );
  wxTextInputStream text( input );
  wxUint8 i1;
  float f2;
  wxString line;

  text >> i1;       // read a 8 bit integer.
  text >> i1 >> f2; // read a 8 bit integer followed by float.
  text >> line;     // read a text line
Which needs the include file <wx/txtstrm.h>. When i try that the compiler says wxFileInputStream and wxTextInputStream havent been declared.

The last link http://docs.wxwidgets.org/stable/wx_wxfiledialog.html I dont know what to use to get information from a text file to a string:

wxFileDialog::wxFileDialog
wxFileDialog::~wxFileDialog
wxFileDialog::GetDirectory
wxFileDialog::GetFilename
wxFileDialog::GetFilenames
wxFileDialog::GetFilterIndex
wxFileDialog::GetMessage
wxFileDialog::GetPath
wxFileDialog::GetPaths
wxFileDialog::GetWildcard
wxFileDialog::SetDirectory
wxFileDialog::SetFilename
wxFileDialog::SetFilterIndex
wxFileDialog::SetMessage
wxFileDialog::SetPath
wxFileDialog::SetWildcard
wxFileDialog::ShowModal

None of them look like anything I can use to do it with.

And my c++ programming book hasn't got anything in about file input output.
I dont know enough to know whats wrong with the code I have tried, and all of it hasn't worked.

Can anyone please post a quick snippet of code that will get everything from a text file into a string?

This will really help me out, I can't do it by myself (yet), but I am right at the end of my program, and I was hoping to finish it off.

Thanks

Mark

JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway » Wed Apr 23, 2008 9:43 pm

Hi Mark,
This didn't help, theText was not declared,
I'm guessing theText is a wxTextCtrl object.
In any case, if you want to read strings, check out wxTextFile http://docs.wxwidgets.org/2.6/wx_wxtextfile.html

Fairly straight forward to use:

Code: Select all

wxTextFile aFile;
aFile.Open(_wxT("FileToOpen.txt"));
if (aFile.IsOpened()) {

  for ( str = aFile.GetFirstLine(); !aFile.Eof(); str = aFile.GetNextLine() )
  {
      // do something with the current line in str
  }
}
If you are not saving text, then wxFile could be useful to you too.

Jim
OS: Vista SP1, wxWidgets 2.8.7.

Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria » Thu Apr 24, 2008 1:07 am

I think you're confusing what does what, wxFileDialog only shows the file dialog, it doesn't do any loading. What you need to do is use the GetPath() method to get the path to the file that was selected, then you can read this file using I/O classes

mrmarky2
Earned some good credits
Earned some good credits
Posts: 113
Joined: Fri Apr 04, 2008 1:02 pm

Post by mrmarky2 » Thu Apr 24, 2008 2:51 pm

Yeh, I think i came in out of my depth. But I think I have it working now. :D

Thanks for the code Jim, and everyone else who helped :)

After this i'm gonna start learning c++ 8)

Mark

Post Reply