What does wxFFile.Open expect as parameter? 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
Geo
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jul 08, 2006 7:59 pm
Location: Mexico
Contact:

What does wxFFile.Open expect as parameter?

Post by Geo »

The documentation indicates 3 different constructors for a wxFFile:

Code: Select all

wxFFile()
wxFFile(const char* filename, const char* mode = "r")
wxFFile(FILE* fp)
But, when trying to compile this pice of code:

Code: Select all

  wxString fileName = wxT( "file.ext" );

  wxFFile file( (const char*)fileName.mb_str() );
  if ( file.IsOpened() ) {
    //...
  }
  else {
    //...
  }
I get this compiler error (it's not the exact compiler output, I translated it to English :P):

[quote]
error: no coincident function call for
The limit is your imagination.
Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T »

How about fileName.fn_str() , this is supposed to return the correct representation for your OS.

Sof.T
The home of Sof.T http://www.sof-t.site88.net/
Author of Programming with wxDevC++
http://sourceforge.net/projects/wxdevcpp-book/
benedicte
wxWorld Domination!
wxWorld Domination!
Posts: 1409
Joined: Wed Jan 19, 2005 3:44 pm
Location: Paris, France

Post by benedicte »

If you look at the headers (not the documentation), the wxFFile::Open function signature is the following:

Code: Select all

    // open a file (existing or not - the mode controls what happens)
  bool Open(const wxChar *filename, const wxChar *mode = _T("r"));
You should be able to call it using:

Code: Select all

wxFFile my_file;
wxString my_filename (wxT("C:\\temp\\test.txt"));
my_file.Open (my_filename.fn_str());
Geo
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jul 08, 2006 7:59 pm
Location: Mexico
Contact:

Post by Geo »

benedicte wrote: You should be able to call it using:

Code: Select all

wxFFile my_file;
wxString my_filename (wxT("C:\\temp\\test.txt"));
my_file.Open (my_filename.fn_str());
I tried this:

Code: Select all

#include <wx/wx.h>
#include <wx/string.h>
#include <wx/ffile.h>
#include <iostream>
using std::cout;
using std::endl;

int main() {
	wxFFile my_file;
	wxString my_filename( wxT( "fondo.bmp" ) );
	my_file.Open( my_filename.fn_str() );

	return 0;
}
And got:
testfile.cpp: In function
The limit is your imagination.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

what compiler are you using? I can just use:

Code: Select all

wxFFile my_file( wxT("C:\\temp\\test.txt") );
or

Code: Select all

wxString filename=wxT("C:\\temp\\test.txt");
wxFFile my_file(filename);
without any complicated constructs or casts.
Geo
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat Jul 08, 2006 7:59 pm
Location: Mexico
Contact:

Post by Geo »

Actually, I got into trouble when I tried to use the file returned by a wxFileDialog as a parameter for a C function I've made sometime ago. This function expected a file name as a const char*, and I couldn't cast the wxString to it (I opened other post about this conversion).

Then I found about the wxFFile class, the docs described it as a wrapper around FILE, but the docs didn't indicate a constructor that could receive a wxStrint as parameter, even, the Open function clearle indicates it expects a const char*. The problem is that when compiling, the compiler was indicating me that the Open function was expecting a wxChar*, not a char*, that's why I got confused, the docs said something and the real stuff is different?

Anyway, now I think I can go on with my code, thank you so much for your comments.

Regards,
JJ (Geo):
The limit is your imagination.
Avi
Super wx Problem Solver
Super wx Problem Solver
Posts: 398
Joined: Mon Aug 30, 2004 9:27 pm
Location: Tel-Aviv, Israel

Post by Avi »

You seem to be using a Unicoded wxWidgets build... So simply do what doublemax said... The docs have a wrong char*s everywhere... They need to update the docs to wxChar*s... ;)
Post Reply