Solved

CFile::Open returns FALSE

Posted on 1998-05-11
9
689 Views
Last Modified: 2013-11-20
Hi,
I use the CFile class to read a file. This works fine! I can read the file. But: Open returns FALSE.
The TRACE macro prints "Can't open file filename, error = 0"!
fileException.m_cause = 0 means CFileException::none =>  No error occurred.
So whats wrong?
 
CFile  *pcFile = new CFile();
CFileException fileException;

if ( !pcFile->Open( lpszFileName, CFile::modeReadWrite | CFile::typeBinary ),  &fileException )
{
      TRACE( "Can't open file %s, error = %u\n", lpszFileName, fileException.m_cause );
}
0
Comment
Question by:dib
  • 4
  • 3
  • 2
9 Comments
 
LVL 1

Accepted Solution

by:
Blondie050798 earned 100 total points
ID: 1309404
If you look at the CFile constructor help it will tell you what is wrong:

CFile::typeBinary   Sets binary mode (used in derived classes only).


0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1309405
Have'nt you written the Open call wrong here ???

if ( !pcFile->Open( lpszFileName, CFile::modeReadWrite | CFile::typeBinary ),  &fileException )
       

should proberly be:

if ( !pcFile->Open( lpszFileName, CFile::modeReadWrite | CFile::typeBinary, &fileException )

0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1309406
ONE MORE TRY ()("¤#&)"¤:
Should proberly be:

if ( !pcFile->Open( lpszFileName, CFile::modeReadWrite | CFile::typeBinary, &fileException ))
{
     
}


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1309407
As piano points out (but didn't explain) you've put the ',' outside the list of function args.  This is quite valid C/C++, but is NOT what you wanted.  Instead of being a function arg separator the ',' is now treated as a comma operator.  The commad operator evaluates and its left-hand-side, and then evaluates the right-hand-side and returns the right-hand-side value as the overall value.  So your code, as you've written is equivalent to:

pcFile->Open( lpszFileName, CFile::modeReadWrite | CFile::typeBinary );
if (&fileException ) {
      TRACE( "Can't open file %s, error = %u\n", lpszFileName, fileException.m_cause );
}

And as '&fileException' is non-null, the 'if' always does the TRACE statement.

I hope this clears up your problem and explains what piano_boxer was saying.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1309408
Blondie's answer is INCORRECT .. it does NOT explain the problem.

The use of typeBinary with CFile is NOT a problem .. these flags are simple ignored.  Indeed, the CFile::Open acutally masks out this flag.

piano_boxer's answer was correct.

Please ignore Blondie's answer when reading this PAQ

0
 
LVL 1

Expert Comment

by:Blondie050798
ID: 1309409
CFile::typeBinary should not be in the code, but like you say it should not cause a 'problem'...

I pointed this out because it was in direct conflict with the documentation; I thought that the brackets were simply a typo, and hence didn't point that out.

piano_boxer's answer was correct....but don't bother with the binary flag! :-)
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1309410
Glad that's cleared up.

The documentation is a little misleading in this case.  typeBinary and typeText are (primarily) used for CStdioFile (which is derived from CFile).  That way you can open a CStdioFile in either binary or text.  They are not invalid for a CFile, but just don't have any meaning as a CFile is ALWAYS opened binary .. the typeBinary is ignored.

You may want to use CStdioFile instead of CFile (and keep the typeBinary flag) .. there is very little difference between CStdioFile and CFile .. just the addition of string/line i/o and the option of text/binary mode.

0
 
LVL 1

Expert Comment

by:Blondie050798
ID: 1309411
Also CStdioFile is buffered whilst CFile isn't.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1309412
True .. hadn't mentioned that, I was thinking more of available member functions .. buffering can make a performance difference, so using CStdioFile instead of CFile may be the better solution anyway

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now