wxfile, wxtextfile, something... just help me! 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
npsken
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Jun 13, 2008 11:00 am

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

Post 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!
Double Trouble
Knows some wx things
Knows some wx things
Posts: 26
Joined: Sat Jun 14, 2008 12:42 pm
Location: Sweden
Contact:

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

Post 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();
}
npsken
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Jun 13, 2008 11:00 am

Post 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post 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;
}
mc2r
wxWorld Domination!
wxWorld Domination!
Posts: 1195
Joined: Thu Feb 22, 2007 4:47 pm
Location: Denver, Co
Contact:

Post 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
npsken
In need of some credit
In need of some credit
Posts: 6
Joined: Fri Jun 13, 2008 11:00 am

Post 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!
kayamel
Knows some wx things
Knows some wx things
Posts: 42
Joined: Wed May 24, 2006 2:38 pm

Post 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
Post Reply