I have try passing a wxString path to a c function locally, but it won't work. If i try passing it globally it will work. How do i pass a wxString to a c function.
At the top, that how i declare,
void extract(wxString path);
void testFrm::WxToolButton1Click(wxCommandEvent& event)
{
if (WxOpenFileDialog1->ShowModal() == wxID_OK)
{
path = WxOpenFileDialog1->GetPath();
wxMessageBox(path);
extract(path);
}
}
void extract(wxString path)
{
...
}
So any error with that?
how to pass a string to a c function Topic is solved
Your code seems to be ok, i dont see any problem with that.
If your issue is getting a string out of wxString, then use c_str()
or just pass c_string into the function
then use c_string in whatever you want it for.
If your issue is getting a string out of wxString, then use c_str()
Code: Select all
void extract(wxString path)
{
const char* c_string = path.c_str();
...
}
then use c_string in whatever you want it for.
-
- Can't get richer than this
- Posts: 802
- Joined: Mon Dec 27, 2004 10:46 am
- Location: Estonia, Tallinn
- Contact:
c_str() is only suitable to use for getting a wxChar out of a wxString. That means char* OR a wchar_t* based on the build type.
(const char*)mb_str(conv_class) is for always getting a char* regardless of wx build type.
(const wchar_t*)wc_str(...) is for always getting a wchar_t* out of a wxString regardless of wx build type.
extract isn't a C function if it's in the .cpp file. You are not wrapping it inside extern "C". It is taking a wxString there, and that's an instance. However, you are making a copy there, which might be unnecessary, and usually is. I suggest looking up what references are in C++.
(const char*)mb_str(conv_class) is for always getting a char* regardless of wx build type.
(const wchar_t*)wc_str(...) is for always getting a wchar_t* out of a wxString regardless of wx build type.
extract isn't a C function if it's in the .cpp file. You are not wrapping it inside extern "C". It is taking a wxString there, and that's an instance. However, you are making a copy there, which might be unnecessary, and usually is. I suggest looking up what references are in C++.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD
Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
OS's: Gentoo Linux, WinXP; WX: CVS HEAD
Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
-
- Can't get richer than this
- Posts: 802
- Joined: Mon Dec 27, 2004 10:46 am
- Location: Estonia, Tallinn
- Contact:
I don't understand what problem you are having.
The code seems generally fine. I'd certainly make the wxString argument of extract a reference, of course.
I also do not see any C involved here, other than the fact that extract is a function (that takes a C++ class instance as an argument, but that's basically like a struct), and not a static method of a class.
Can you explain what problem are you having, if any? Any compiler error, anything?
The code seems generally fine. I'd certainly make the wxString argument of extract a reference, of course.
I also do not see any C involved here, other than the fact that extract is a function (that takes a C++ class instance as an argument, but that's basically like a struct), and not a static method of a class.
Can you explain what problem are you having, if any? Any compiler error, anything?
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD
Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
OS's: Gentoo Linux, WinXP; WX: CVS HEAD
Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
Thanks for helping! I realise that I made a mistake by declaring the function prototype at the wrong place. This is how it should be place. Thanks for all your help.
..........
#include "testFrm.h"
#define filewritten "rmsd.txt"
#define filewritten1 "mean.txt"
long checkrunsample(long *,long *,wxString path);
void extract(wxString path);
//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
#include "WxToolButton3_XPM.xpm"
#include "WxToolButton2_XPM.xpm"
#include "WxToolButton1_XPM.xpm"
////Header Include End
//----------------------------------------------------------------------------
// testFrm
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
..............
After declaring the function below #include "testFrm.h" everything is working perfectly. Before i declare a function before #include "testFrm.h", it give a lot of error. Anyway thanks for telling me to look at the error. Probably, I was too tired at the time. Sorry.
..........
#include "testFrm.h"
#define filewritten "rmsd.txt"
#define filewritten1 "mean.txt"
long checkrunsample(long *,long *,wxString path);
void extract(wxString path);
//Do not add custom headers
//wxDev-C++ designer will remove them
////Header Include Start
#include "WxToolButton3_XPM.xpm"
#include "WxToolButton2_XPM.xpm"
#include "WxToolButton1_XPM.xpm"
////Header Include End
//----------------------------------------------------------------------------
// testFrm
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
..............
After declaring the function below #include "testFrm.h" everything is working perfectly. Before i declare a function before #include "testFrm.h", it give a lot of error. Anyway thanks for telling me to look at the error. Probably, I was too tired at the time. Sorry.
or you may use <string> instead
it is part of STL
i think it probably may resolve your problem
i think it probably may resolve your problem