Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all:-)

I want to open a binary file using the CFile-class:

CFile f;
CFileException ex;
if(!f.Open(m_sPathToForm, CFile::modeRead | CFile::typeBinary), &ex)
{
        TCHAR szError[1024];
	ex.GetErrorMessage(szError, 1024);
	return -1;
}


But open fails with the following error message: CFileException::none, which has to following text: "No error occurred."

Has anyone an idea, why the open-function could fail?
Are there binary files which cannot be opened by the CFile class?

thx for any help!
Posted

CPallini and SA have pointed out your errors quite correctly which should lead to the solution. The code you have presented is taken from here incorrectly: https://msdn.microsoft.com/en-us/library/hwbccf8z.aspx[^].

Note:
C++
if (!sourceFile.Open(pszSource,
   CFile::modeRead | CFile::shareDenyWrite, &ex))


Revise your code using the example given in the link and go from there.
 
Share this answer
 
v2
Comments
CPallini 22-Apr-15 3:07am    
Good catch, my 5.
You did not handle any exceptions and cannot say if there is an exception or not. Your exception information is not really related to any exceptions, this is just the default state of your ex object, which is something you absolutely don't need.

From this code, it's quite apparent that you have no clue what exceptions are and how to use them. You have to learn this topic from scratch. See, for example, http://www.cplusplus.com/doc/tutorial/exceptions[^].

—SA
 
Share this answer
 
v2
Comments
Member 10781325 21-Apr-15 12:16pm    
hmmm... I think that i know what exceptions are;-)

Its just, that CFile::Open never throws an exception: read this: (https://msdn.microsoft.com/en-us/library/hwbccf8z.aspx)

While the CFile constructor will throw an exception in an error condition, Open will return FALSE for error conditions. Open can still initialize a CFileException object to describe the error, however. If you don't supply the pError parameter, or if you pass NULL for pError, Open will return FALSE and not throw a CFileException. If you pass a pointer to an existing CFileException, and Open encounters an error, the function will fill it with information describing that error. In neither case will Open throw an exception.
Sergey Alexandrovich Kryukov 21-Apr-15 12:33pm    
Oh, it means that the situation is a bit worse than that: I thought you have little knowledge on the topic, say, about zero, but you probably have illusionary knowledge well below zero, which is also harder to get rid of, in comparison with getting new knowledge. I provided a reference for you.

Now, what you are talking about has nothing to do with exceptions. The exception is not thrown because Windows API was created mainly before exceptions, and, more importantly, because it supposed to server the application code written in languages where exceptions are not implemented. But you are not throwing any exceptions either. Your "ex" object just makes no sense... :-)

—SA
Exceptions are possibly your second problem, the very first one is the C/C++ comma operator (see, for instance: "Comma operator" at Wikipedia[^]). The expression inside the if braces evaluates to non-zero because &ex, being the valid aadress of an object, is not NULL: try the following code
C++
#include <iostream>
using namespace std;
int main()
{
  if ( 0, 5)
    cout << "hello" << endl;

}
 
Share this answer
 
Comments
Member 10781325 22-Apr-15 2:32am    
Thx, was just a typing-error:-) didnt see that yester day evening:-)
thx all
CPallini 22-Apr-15 3:06am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900