Hi
I've got the following code for the constructor of a
class that I've derived from CFile. It is in a project
that I've created using ATL COM AppWizard, with MFC
support. So it's using ATL *and* MFC.
However when I try to write to the file, it asserts in
the first line of the CFile::Write class, on the line:
ASSERT (m_hFile != hFileNull)

so I decided to put this test in my own class so I could
check for it - my code is as follows:

CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
{
try
{
CFile::CFile(lpszFilename,
CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
if(m_hFile != hFileNull)
OpenedOK = true;
else
OpenedOK = false; //<-this occurs.
}
catch(CFileException e)
{
afxDump << "File could not be opened " <<
e.m_cause << "\n";
OpenedOK = false;
}

}

However I checked in windows explorer and the file IS
being created. How can I get the file handle to be non-
null, and thus enable writing to the file? thanks

Re: CFile class newbie question - file handle invalid? by terry

terry
Sun Dec 14 19:32:31 CST 2003

Shouldnt u be instantiating m_hFile somewhere. It should either be returned
by the file create function or passed as a parameter to the file create
function.

T
"Bonj" <anonymous@discussions.microsoft.com> wrote in message
news:01ba01c3c2a5$4c94d300$a401280a@phx.gbl...
> Hi
> I've got the following code for the constructor of a
> class that I've derived from CFile. It is in a project
> that I've created using ATL COM AppWizard, with MFC
> support. So it's using ATL *and* MFC.
> However when I try to write to the file, it asserts in
> the first line of the CFile::Write class, on the line:
> ASSERT (m_hFile != hFileNull)
>
> so I decided to put this test in my own class so I could
> check for it - my code is as follows:
>
> CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
> {
> try
> {
> CFile::CFile(lpszFilename,
> CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
> if(m_hFile != hFileNull)
> OpenedOK = true;
> else
> OpenedOK = false; //<-this occurs.
> }
> catch(CFileException e)
> {
> afxDump << "File could not be opened " <<
> e.m_cause << "\n";
> OpenedOK = false;
> }
>
> }
>
> However I checked in windows explorer and the file IS
> being created. How can I get the file handle to be non-
> null, and thus enable writing to the file? thanks



Re: CFile class newbie question - file handle invalid? by Bonj

Bonj
Mon Dec 15 03:46:16 CST 2003

Do you know how I would go about doing that? I don't know
about any 'file create' function, I'm specifying the name
and mode of the file in the constructor.

I don't suppose anybody knows of an example of using CFile
to write to a binary file (hopefully by deriving a class
from it)?


>-----Original Message-----
>Shouldnt u be instantiating m_hFile somewhere. It should
either be returned
>by the file create function or passed as a parameter to
the file create
>function.
>
>T
>"Bonj" <anonymous@discussions.microsoft.com> wrote in
message
>news:01ba01c3c2a5$4c94d300$a401280a@phx.gbl...
>> Hi
>> I've got the following code for the constructor of a
>> class that I've derived from CFile. It is in a project
>> that I've created using ATL COM AppWizard, with MFC
>> support. So it's using ATL *and* MFC.
>> However when I try to write to the file, it asserts in
>> the first line of the CFile::Write class, on the line:
>> ASSERT (m_hFile != hFileNull)
>>
>> so I decided to put this test in my own class so I could
>> check for it - my code is as follows:
>>
>> CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
>> {
>> try
>> {
>> CFile::CFile(lpszFilename,
>> CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary);
>> if(m_hFile != hFileNull)
>> OpenedOK = true;
>> else
>> OpenedOK = false; //<-this occurs.
>> }
>> catch(CFileException e)
>> {
>> afxDump << "File could not be opened " <<
>> e.m_cause << "\n";
>> OpenedOK = false;
>> }
>>
>> }
>>
>> However I checked in windows explorer and the file IS
>> being created. How can I get the file handle to be non-
>> null, and thus enable writing to the file? thanks
>
>
>.
>

Re: CFile class newbie question - file handle invalid? by sbogus

sbogus
Mon Dec 15 08:44:54 CST 2003

Hi Bonj,
your code is probably wrong. Use this piece instead...

CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename) throw
(CFileException)
: CFile(lpszFilename,CFile::modeCreate | CFile::modeWrite |
CFile::typeBinary)
{
// You don't need a try...catch block here, since if there's something
wrong, the constructor will throw the exception,
// or one of the base class constructors will do that work for you.
if(m_hFile != hFileNull)
OpenedOK = true; // this should occur
else
{
OpenedOK = false;
throw new CFileException();
}
}

May be gurus of c++ language will condemn my "confusity", but I think the
explicit call of the constructor method you make in your inherited class
does not do what you want to do - executing code from your constructor means
all the constructors of the base classes are called (in this case the
default constructors) and in particular, your m_hFile member is really
NULL-ed by the CFile's default constructor. Calling the CFile constructor
will in my opinion initialize a temporary object only and will not
initialize the inherited data members. May be I'm wrong... Anyway, the above
piece of code will work for you.

Hope to helped to you.

Kind regards,
sbogus_confused



"terry" <back@ya.com> schrieb im Newsbeitrag
news:brj2vp$1kt$1@news7.svr.pol.co.uk...
> Shouldnt u be instantiating m_hFile somewhere. It should either be
returned
> by the file create function or passed as a parameter to the file create
> function.
>
> T
> "Bonj" <anonymous@discussions.microsoft.com> wrote in message
> news:01ba01c3c2a5$4c94d300$a401280a@phx.gbl...
> > Hi
> > I've got the following code for the constructor of a
> > class that I've derived from CFile. It is in a project
> > that I've created using ATL COM AppWizard, with MFC
> > support. So it's using ATL *and* MFC.
> > However when I try to write to the file, it asserts in
> > the first line of the CFile::Write class, on the line:
> > ASSERT (m_hFile != hFileNull)
> >
> > so I decided to put this test in my own class so I could
> > check for it - my code is as follows:
> >
> > CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
> > {
> > try
> > {
> > CFile::CFile(lpszFilename,
> > CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
> > if(m_hFile != hFileNull)
> > OpenedOK = true;
> > else
> > OpenedOK = false; //<-this occurs.
> > }
> > catch(CFileException e)
> > {
> > afxDump << "File could not be opened " <<
> > e.m_cause << "\n";
> > OpenedOK = false;
> > }
> >
> > }
> >
> > However I checked in windows explorer and the file IS
> > being created. How can I get the file handle to be non-
> > null, and thus enable writing to the file? thanks
>
>



Re: CFile class newbie question - file handle invalid? by Alexander

Alexander
Mon Dec 15 14:19:14 CST 2003

You are absolutely right. One can't catch the exceptions in the
constructors of its base classes within ones constructor. And the
original code was instantiating a temporary CFile object. However,
I don't consider myself a C++ guru...

--=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

"sbogus" <sbogus6@hotmail.com> wrote in message =
news:OKdmCoxwDHA.2396@TK2MSFTNGP09.phx.gbl...
> Hi Bonj,
> your code is probably wrong. Use this piece instead...
>=20
> CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename) throw
> (CFileException)
> : CFile(lpszFilename,CFile::modeCreate | CFile::modeWrite |
> CFile::typeBinary)
> {
> // You don't need a try...catch block here, since if there's =
something
> wrong, the constructor will throw the exception,
> // or one of the base class constructors will do that work for =
you.
> if(m_hFile !=3D hFileNull)
> OpenedOK =3D true; // this should occur
> else
> {
> OpenedOK =3D false;
> throw new CFileException();
> }
> }
>=20
> May be gurus of c++ language will condemn my "confusity", but I think =
the
> explicit call of the constructor method you make in your inherited =
class
> does not do what you want to do - executing code from your constructor =
means
> all the constructors of the base classes are called (in this case the
> default constructors) and in particular, your m_hFile member is really
> NULL-ed by the CFile's default constructor. Calling the CFile =
constructor
> will in my opinion initialize a temporary object only and will not
> initialize the inherited data members. May be I'm wrong... Anyway, the =
above
> piece of code will work for you.
>=20
> Hope to helped to you.
>=20
> Kind regards,
> sbogus_confused
>=20
>=20
>=20
> "terry" <back@ya.com> schrieb im Newsbeitrag
> news:brj2vp$1kt$1@news7.svr.pol.co.uk...
> > Shouldnt u be instantiating m_hFile somewhere. It should either be
> returned
> > by the file create function or passed as a parameter to the file =
create
> > function.
> >
> > T
> > "Bonj" <anonymous@discussions.microsoft.com> wrote in message
> > news:01ba01c3c2a5$4c94d300$a401280a@phx.gbl...
> > > Hi
> > > I've got the following code for the constructor of a
> > > class that I've derived from CFile. It is in a project
> > > that I've created using ATL COM AppWizard, with MFC
> > > support. So it's using ATL *and* MFC.
> > > However when I try to write to the file, it asserts in
> > > the first line of the CFile::Write class, on the line:
> > > ASSERT (m_hFile !=3D hFileNull)
> > >
> > > so I decided to put this test in my own class so I could
> > > check for it - my code is as follows:
> > >
> > > CBMPFileInternal::CBMPFileInternal(LPCTSTR lpszFilename)
> > > {
> > > try
> > > {
> > > CFile::CFile(lpszFilename,
> > > CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
> > > if(m_hFile !=3D hFileNull)
> > > OpenedOK =3D true;
> > > else
> > > OpenedOK =3D false; //<-this occurs.
> > > }
> > > catch(CFileException e)
> > > {
> > > afxDump << "File could not be opened " <<
> > > e.m_cause << "\n";
> > > OpenedOK =3D false;
> > > }
> > >
> > > }
> > >
> > > However I checked in windows explorer and the file IS
> > > being created. How can I get the file handle to be non-
> > > null, and thus enable writing to the file? thanks
> >
> >
>=20
>

Re: CFile class newbie question - file handle invalid? by Igor

Igor
Tue Dec 16 10:47:16 CST 2003

One can, with function try block:

Derived::Derived()
try
: Base()
{
// ...
}
catch (MyException&)
{
// exceptions from Base constructor caught here
}

VC6 does not support function try blocks, VC7.1 does.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"Alexander Nickolov" <agnickolov@mvps.org> wrote in message
news:e$TQSi0wDHA.1576@TK2MSFTNGP11.phx.gbl...
You are absolutely right. One can't catch the exceptions in the
constructors of its base classes within ones constructor. And the
original code was instantiating a temporary CFile object. However,
I don't consider myself a C++ guru...