Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CFile::Open returns FALSE

Posted on 1998-05-11
9
Medium Priority
?
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

610 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