Solved

CFile::Open returns FALSE

Posted on 1998-05-11
9
686 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 67
Detect CR LF to each line 12 137
java ^ examples 8 57
Thin secure Windows 10 5 49
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

9 Experts available now in Live!

Get 1:1 Help Now