Probably a simple problem

Hi I’m trying to get a GUI working with the root classes and I’ve run aground of a problem I just can’t solve. It might be merely a basic C++ problem that I’m simply missing, I’ve been staring at the code for too long.

As per the GUI example I’m creating a “Handle Menu item” method.
The method works fine, the GUI window pops up and the user can select the file they’re interested in loading.
HandleMenu() prints out the full path and filename no problem when called.
What I’m having difficulty to access is that path and filename in other methods.
Basically I created a const char called fFileLoaded that is a private data member of MyMainFrame and for whatever reason it’s returning blank in all other methods except HandleMenu.
Here follows the HandleMenu code and the header to MyMainFrame:

[code]void MyMainFrame::HandleMenu(Int_t id)
{
// Handle menu items
switch (id)
{
case M_FILE_LOAD:
{
static TString dir(".");
TGFileInfo fi;
fi.fFileTypes = filetypes;
fi.fIniDir = StrDup(dir.Data());
printf(“fIniDir = %s\n”, fi.fIniDir);
new TGFileDialog(gClient->GetRoot(), fMain, kFDOpen, &fi);
printf(“Loaded file: %s (dir: %s)\n”, fi.fFilename, fi.fIniDir);
dir = fi.fIniDir;
char c[200];
sprintf(c,"%s", fi.fFilename);
cout <<"Value of c is: "<< c << endl;
fFileLoaded = c;
cout << "Value of fFileLoaded is: " << fFileLoaded << endl;
}
break;
case M_TEST_SEARCH:

default:
printf(“Menu item %d selected\n”,id);
break;
}
}

#include <TGFrame.h>
#include <TGMenu.h>
#include “TRightHandFit.h”
#include “TPeakFeeder.h”

#ifndef MYMAINFRAME_H
#define MYMAINFRAME_H

/*
enum ETestCommandIdentifiers{M_FILE_LOAD};
const char filetypes[] = { “All files”, "",
“ROOT files”, “.root",
“ROOT macros”, "
.C”,
“Text files”, “*.[tT][xX][tT]”,
0, 0 };
*/

class TRootEmbeddedCanvas;

class MyMainFrame {

public:
MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyMainFrame();
// slots
void DoDraw();
void AutoCalibrate();
void HandleMenu(Int_t id);
void HandlePopup(){ printf(“menu popped up\n”);}
void HandlePopdown(){printf(“menu popped down\n”);};
private:
const char *fFileLoaded;
TGMainFrame *fMain;
TRootEmbeddedCanvas *fEcanvas;
TGLayoutHints *fMenuBarLayout;
TGLayoutHints *fMenuBarItemLayout;
TGLayoutHints *fMenuBarHelpLayout;
TGMenuBar *fMenuBar;
TGPopupMenu *fMenuFile;
TGPopupMenu *fMenuTest;
TGPopupMenu *fMenuHelp;

ClassDef(MyMainFrame,0)
};
#endif
[/code]
Can anyone tell me what I’m doing wrong? Does it have to do with peculiarities of TGFileInfo?Any help would be much appreciated!

Your variable char c[200] is defined in a scope block. Then, when going out of scope, it is not valid anymore.
A solution would be to change the type of fFileLoaded as following :

replace :

const char *fFileLoaded;

by :

TString fFileLoaded;

It will work.

Cheers,
Bertrand.

Thanks so much! #-o