Discussion:
open file using wxFileDialog and fopen
Fabian Braennstroem
19 years ago
Permalink
Hi,

I am pretty new to C++ and wx programing and try to open a
data file using wxFileDialog and fopen.

I am able to choose a file using the lines from an example,
but how can I adjust the filename of fopen for using fscanf
later on?


wxFileDialog dialog
(
this,
_T("Testing open file dialog"),
wxEmptyString,
wxEmptyString,
#ifdef __WXMOTIF__
_T("C++ files (*.log)|*.log")
#else
_T("C++ files (*.log;*.h)|*.log;*.h")
#endif
);

dialog.SetDirectory(wxGetHomeDir());

// Message Nach dem Öffnen
if (dialog.ShowModal() == wxID_OK)
{
wxString info;
info.Printf(_T("Full file name: %s\n")
_T("Path: %s\n")
_T("Name: %s"),
dialog.GetPath().c_str(),
dialog.GetDirectory().c_str(),
dialog.GetFilename().c_str());
wxMessageDialog dialog2(this, info, _T("Selected file"));


dialog2.ShowModal();
}

fz=fopen(dialog.GetFilename().c_str(),"r");
cout << "output: " << dialog.GetFilename().c_str() << endl;

Even the last cout statement just prints the memory address, but
not the file name.

Do you have an idea?

Greetings!
Fabian
Fabian Braennstroem
19 years ago
Permalink
Hi,
...
O.k. I just found a bit more. I saw the 'console' example
with 'testfileread' and tried:

wxFile file(_T(dialog.GetFilename().c_str()));

To declare the filename; compiling it I get:

tec2ens_glade.cpp: In member function 'void
MyFrame::ChooseFile(wxCommandEvent&)': tec2ens_glade.cpp:109: error: 'Ldialog' was not declared in this scope

Could you give me a hint?


I actually try to accomplish to read data values from a file
with a structure like this:

x1 y1 z1 u1 v1 w1 p1
x2 y2 z2 u2 v2 w2 p2
x3 y3 z3 u3 v3 w3 p3
...................

Using 'normal' fscanf I could do:

for (k=0;k<kt-1; k++)
{
for (j=0;j<jt-1; j++)
{
for (i=0;i<it-1; i++)
{
fscanf(fz,"%E %E %E %E %E %E %E\n",&x[i][j][k],&y[i][j][k],&z[i][j][k],&u[i][j][k],&v[i][j][k],&w[i][j][k],&p[i][j][k]);

Do you have an idea how I could do it using the wxFile
class?

Greetings!
Fabian
Erik Jensen
19 years ago
Permalink
Post by Fabian Braennstroem
Hi,
I am pretty new to C++ and wx programing and try to open a
data file using wxFileDialog and fopen.
I am able to choose a file using the lines from an example,
but how can I adjust the filename of fopen for using fscanf
later on?
wxFileDialog dialog
(
this,
_T("Testing open file dialog"),
wxEmptyString,
wxEmptyString,
#ifdef __WXMOTIF__
_T("C++ files (*.log)|*.log")
#else
_T("C++ files (*.log;*.h)|*.log;*.h")
#endif
);
dialog.SetDirectory(wxGetHomeDir());
// Message Nach dem Öffnen
if (dialog.ShowModal() == wxID_OK)
{
wxString info;
info.Printf(_T("Full file name: %s\n")
_T("Path: %s\n")
_T("Name: %s"),
dialog.GetPath().c_str(),
dialog.GetDirectory().c_str(),
dialog.GetFilename().c_str());
wxMessageDialog dialog2(this, info, _T("Selected file"));
dialog2.ShowModal();
}
fz=fopen(dialog.GetFilename().c_str(),"r");
cout << "output: " << dialog.GetFilename().c_str() << endl;
FB> O.k. I just found a bit more. I saw the 'console' example
FB> with 'testfileread' and tried:

FB> wxFile file(_T(dialog.GetFilename().c_str()));

FB> To declare the filename; compiling it I get:

FB> tec2ens_glade.cpp: In member function 'void
FB> MyFrame::ChooseFile(wxCommandEvent&)':
FB> tec2ens_glade.cpp:109: error: 'Ldialog' was not declared in this
FB> scope

FB> Could you give me a hint?
the _T() macro is for converting literals like "hello world"

If you're using wxWidgets functions, you hardly ever need the
"c.str()", you can use the wxString object right away.
e.g.: wxFile myfile(dialog.GetPath());


FB> I actually try to accomplish to read data values from a file
FB> with a structure like this:

FB> x1 y1 z1 u1 v1 w1 p1
FB> x2 y2 z2 u2 v2 w2 p2
FB> x3 y3 z3 u3 v3 w3 p3
FB> ...................

FB> Using 'normal' fscanf I could do:

FB> for (k=0;k<kt-1; k++)
FB> {
FB> for (j=0;j<jt-1; j++)
FB> {
FB> for (i=0;i<it-1; i++)
FB> {
FB> fscanf(fz,"%E %E %E %E %E %E
FB> %E\n",&x[i][j][k],&y[i][j][k],&z[i][j][k],&u[i][j][k],&v[i][j][k],&w[i][j][k],&p[i][j][k]);

FB> Do you have an idea how I could do it using the wxFile
FB> class?
i'd probably use wxTextFile to get the file line-by-line and
wxStringTokenizer to split the line into its segments.

Hope it helps.

Eric
Fabian Braennstroem
19 years ago
Permalink
Hi to both,
...
Thanks for your help! As you see, I have to figure out some
more stuff. I will take a look at both of your
suggestions/example.

Greetings!
Fabian

BobR
19 years ago
Permalink
Fabian Braennstroem wrote in message ...
Post by Fabian Braennstroem
Hi,
I am pretty new to C++ and wx programing and try to open a
data file using wxFileDialog and fopen.
Why do you say 'C++', but you are using 'C' code?

Use 'std::fstream' (not 'fopen') and 'std::getline()' (not 'fscanf'), and/or
the wxWidgets classes.
Post by Fabian Braennstroem
I actually try to accomplish to read data values from a file
Use 'std::vector' or one of the wxWidgets array classes.


// -----------------------------------------------------------------
// Filer.h
// -----------------------------------------------------------------
#ifndef Filer_H
#define Filer_H
// ----------------------------------------------
#include <iostream> // C++
#include <ostream> //std::endl
#include <fstream>
#include <string>
#include <vector>
// --------------------------------------
#include <stdexcept>
class FileErr : public std::runtime_error { public:
FileErr(const std::string &msg = "") : std::runtime_error(msg){}
};
// --------------------------------------
// note: AFAIK vector does not have a virtual destructor, so,
// never use this class thru a base pointer.

class Filer : public std::vector<std::string> {
public:
// Filer(){}
Filer(const char *filename){ Open(filename); }
private: // public: if I use Filer() Ctor, re-use instance, concat files.
void Open(const char *filename) throw(FileErr){
std::ifstream in(filename);
if(!in){
throw FileErr(__FILE__": Open SonOfABitch!");
} //if(!in)
for( std::string line; std::getline(in, line); ){ // [1]
push_back(line);
} //for()
return;
} //Open(const char*)
public:
void Write(std::ostream& out = std::cout) throw(FileErr){
if(!out){
throw FileErr(__FILE__": Write SonOfABitch!");
} //if(!out)
for(const_iterator w = begin(); w != end(); ++w)
out << *w << std::endl;
return;
} //Write(ostream&)
};
// --------------------------------------
#endif //#ifndef Filer_H
// -----------------------------------------------------------------END

// -----------------------------------------------------------------
// FilerTest.cpp
// -----------------------------------------------------------------
#include <iostream> // C++
#include <ostream> //std::endl
#include <fstream>
#include <string>
#include "Filer.h"
// Shows how to read a file, modify it, then write it
// out (can be same file). Maybe it will give you some ideas.

int main(){
using std::cout; // for NG posting
try{
cout<<"_____ Filer.h test _____"<<std::endl;
Filer Afile("Filer.h");
//Filer Afile("ErrFiler.h"); // to test exception.
Afile.Write( cout );
cout<<"\nAfile.size()="<<Afile.size()<<" lines."<<std::endl;
cout<<"Afile.at(1)="<<Afile.at(1)<<std::endl;

std::string FindThis(
"// never use this class thru a base pointer." );
// - the following works, but, needs 'fixing' <G> -
Filer::iterator FindMain( Afile.begin() );
for(size_t a(0); a < Afile.size(); ++a){
if( Afile.at(a).find( FindThis ) != std::string::npos ){
FindMain = Afile.begin() + a;
}
} // for(a)
// - inserts in front of found line -
Afile.insert(FindMain, "//__________ inserted line __________");
Afile.push_back("\n//----- This is a copy -----");

std::ofstream Ofile("CopyFiler.txt");
if( !Ofile){ return EXIT_FAILURE; }
Afile.Write( Ofile );
cout<<"_____ Filer.h test End _____"<<std::endl;
} // try{}
catch(FileErr &x){
cout<<"FileErr &x.what()="<< x.what() <<std::endl;
} // catch(FileErr&)
catch(std::out_of_range &Oor){
cout<<"\ncaught "<<Oor.what()<<std::endl;
}
catch(...){
cout<< "\ncaught something!!" <<std::endl;
} //catch(MyError&)
return 0;
} // main() end
// -----------------------------------------------------------------END


[1] Andrew Koenig, Walter Brown.
--
Bob R
POVrookie
Loading...