Teen Programmers Unite  
 

 

Return to forum top

How to Open a file via API in VC++

Posted by csspcman [send private reply] at February 13, 2002, 05:38:46 PM

I need to read data from some files in text format but when I use CreateFile function it always return this value (0xCCCCCCC) instead of a handle to my file . I couldn't find this code in error codes list in MSDN library . where did I do wrong ?
As a matter of fact this is a C++ program without any GUI
I only included the "windows.h" I want to use some API function .

Any help or suggestions would be appreciated

Posted by taubz [send private reply] at February 13, 2002, 06:46:15 PM

You'd need to use GetLastError to determine the actual error, which is an incredible pain in Windows programming.

Diagnosing the problem would require knowing what parameters you passed to CreateFile. From the MSDN, "If the specified file exists . . . CREATE_ALWAYS or OPEN_ALWAYS" you'll get an error. So, I trust you're not supplying one of those two flags. (I don't understand why for OPEN_ALWAYS.)

You should be doing something like (to read in an existing file)...

CreateFile("filename", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

Hope that helps.

- taubz

Posted by csspcman [send private reply] at February 14, 2002, 09:16:56 AM

I did as u had said but again I got the same as handle .
I checked it with GetLastError too and it returned the same value again (0xCCCCCCC) . But there is no such an error in the list of MSDN . I'm completely confused . Don't u have any other suggestion !

Posted by taubz [send private reply] at February 14, 2002, 06:20:01 PM

In your filename, if you're specifying the path to the file, be sure to escape your backslashes, ie C:\\path\\to\\file. That's thrown me off a few times.

- taubz

Posted by metamorphic [send private reply] at February 17, 2002, 10:31:07 AM

0xCCCCCCC looks a lot like a memory address. prehaps it is returning a file pointer?

Posted by taubz [send private reply] at February 17, 2002, 02:55:40 PM

It would be *very* coincidental for a memory address to have such a nice number. (Normally it would be returning a pointer.)

- taubz

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.