Page 1 of 1

wxfile, wxtextfile, something... just help me!

Posted: Tue Jun 17, 2008 5:40 pm
by npsken
My task is simple: I need to open a text file and write some stuff to it. I was able to do this in the command line with fstream but now in wxwidgets, I am having a lot of trouble.

Can somebody help me here?

Bonus Awesomeness (Not required): If you can show me how to follow the following pseudo code that would be great:

Code: Select all

Check if file exists
  If not:
    Create file
    Write initial data
    Make file remain open for editing
  If so:
    Open file for editing
End Check
Write a line (skipping to a new one with \n)
Thanks!

Re: wxfile, wxtextfile, something... just help me!

Posted: Tue Jun 17, 2008 5:53 pm
by Double Trouble
npsken wrote:My task is simple: I need to open a text file and write some stuff to it. I was able to do this in the command line with fstream but now in wxwidgets, I am having a lot of trouble.

Can somebody help me here?

Bonus Awesomeness (Not required): If you can show me how to follow the following pseudo code that would be great:

Code: Select all

Check if file exists
  If not:
    Create file
    Write initial data
    Make file remain open for editing
  If so:
    Open file for editing
End Check
Write a line (skipping to a new one with \n)
Thanks!
I'm not sure how much I can help. I'm totally new to wxWidgets but maybe I can get you on the right track. I'm just reading directly from the docs: http://docs.wxwidgets.org/stable/wx_wxt ... tfileclose

But I think you should start out with something like this:

Code: Select all



wxTextFile file;

if(file.Exists() == false)
{
	file.Create();
}
if(file.Exists() == true)
{
	file.Open();
}

Posted: Tue Jun 17, 2008 8:22 pm
by Auria

Posted: Tue Jun 17, 2008 8:46 pm
by npsken
I've seen those before except for the middle one but I still can't get this thing to work:

Code: Select all

wxTextFile file1;
file1.Open(_("data.txt"));
file1.AddLine(_("ROAROAROAROAR!")); //For Testing
file1.Write();
file1.Close();
I have a "data.txt" in the same directory as the final executable, but when I press the button (This code is within a button void) nothing happens.

Posted: Wed Jun 18, 2008 12:05 am
by Auria
The following code works fine on my computer :

Code: Select all

#include <iostream>
#include "wx/wx.h"
#include "wx/textfile.h"

class MyApp: public wxApp
{
public:
    virtual bool OnInit();
};


IMPLEMENT_APP(MyApp)


bool MyApp::OnInit()
{
    
    wxTextFile file1(wxT("data.txt"));
    if(not file1.Exists()) file1.Create();
    else file1.Open();
    file1.AddLine(_("ROAROAROAROAR!")); //For Testing
    file1.Write();
    file1.Close();
    
    
    return true;
}

Posted: Wed Jun 18, 2008 12:42 am
by mc2r
npsken wrote:(This code is within a button void) nothing happens.
If this is where the code is why don't you show that code aren't you showing that code as well. Your event tables etc... Since the code there isn't being run it would be a good assumption to check that the event handler isn't being called.

-Max

Posted: Wed Jun 18, 2008 1:46 am
by npsken
mc2r wrote:
npsken wrote:(This code is within a button void) nothing happens.
If this is where the code is why don't you show that code aren't you showing that code as well. Your event tables etc... Since the code there isn't being run it would be a good assumption to check that the event handler isn't being called.

-Max
I actually knew that it was working because the button also does something else right before the file stuff.

Anyway, I tried Auria's code and it wouldn't work BUT it started working when I used the actual executable that was made rather than using the run menu from codeblocks.

Thanks for all your help, guys!

Posted: Wed Jun 18, 2008 11:55 am
by kayamel
Hi,

When you are using the "run" in CodeBlocks, it looks for the attached resources in the project directory and not in the bin directory.

Regards