Re: global variable
"Carlo" <carletto.m@NOSPAMgmail.com> wrote in message
news:%23WWsuox1GHA.1292@TK2MSFTNGP03.phx.gbl...
hi
this is a simple question (i'm new in vc++). i create a vc++ project with
mfc, i need define a FILE* pt as global variable, visible from all my
class and function (different files). Where have i to write the global
variable definition???
carlo
Have you looked at MFC's CFile class? This is the class that is generally
used for file access in MFC applications. It's interface is quite simple and
easy to understand... you do something like:
CFile file;
file.Open (strFileName, flags, ...);
and then
file..Read/Write etc. For instance:
int main (void)
{
CFile file ("c:\\temp\\testfile.txt", CFile::modeWrite |
CFile::modeCreate);
CString str = "SomeText";
file.Write((LPCTSTR)str, str.GetLength () + 1); // Include mull
terminator if you wish.
}
end so on (Read, Write, Close, Seek, GetL:ength + many others...). Of course
there's more to it than what I''ve written down. In fact file operations
using CFile throw exceptions (intentionally) which you are supposed to catch
(and handle) like this:
try
{
CFile file (pFileName, CFile::modeCreate | CFile::modeWrite);
}
catch (CFileException* e )
{
// Example: error handling:
if( e->m_cause == CFileException::fileNotFound )
printf( "ERROR: File not found\n");
}
Although this may seem cumbersome, in fact it's not that much different from
handling the case where FILE* returns NULL and has the advantage that you
can write your main code without any error checking at all and leave it to
your catch handlers to deal with errors as they come along.
For global variables, I think the standard way to introduce them is to add
them to your main CWinApp derived class. So I imagine in your project you
have a class called MyApp (or similar) and you have:
class MyApp : public CWinApp
{
BOOL InitInstance () {/*Do some initializing*/}
INT Run () {/* Run the program, return exit code */}
int ExitInstance () {/*cleaup global varioables and return exit code*/}
public:
MyApp () {/*construct the app*/}
} theApp;
That is the basic structure of an MFC app. So to introduce a "global"
variabvle, I would suggest adding it as a member to your App class as
follows:
class MyApp : public CWinApp
{
BOOL InitInstance () {/*Do some initializing*/}
INT Run () {/* Run the program */}
public:
MyApp () {/*construct the app*/}
// public == global.
CFile m_file;
} theApp;
Then elsewhere in your program you can access the file by simply writing
down "theApp.m_file". You can open the file on InitInstance perhaps and let
it close itself when your program goes out of scope.
Others may have different suggestions, but I think CFile is more in keeping
with MFC design than using a bare global FILE pointer.
P.S.
I should also note that AfxGetApp will return a pointer to your application
class. To get at the file pointer you could cast the return valiue to a
"MyApp" (preferably use dynamic_cast) and then use essentially
"dynamic_cast<MyApp*> (AfxGetApp ())->m_file".
- Alan Carre