[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CFile::Open returns FALSE

Posted on 1998-05-11
9
Medium Priority
?
721 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Integration Management Part 2
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

868 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