fopen and CreateFile
Could someone please explain the difference between fopen and CreateFile to me.
I hadn't seen createfile until recently when browsing these forums. What are there pros and cons of each that I should be aware of?
Here is what I found a MS's site:
It seems to me that this accomplishes basically the same thing as fopen would but obviously allows for other options like: sharing and security as per the remarks.
hFile = CreateFile("MYFILE.TXT", // create MYFILE.TXT
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // no security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL | // normal file
FILE_FLAG_OVERLAPPED, // asynchronous I/O
NULL); // no attr. template
Am I on the right track?
Also, when programming for windows should I generally use createfile or just save this for when I need to use these additional features?
>>> options like: sharing and security as per the remarks.
Indeed. You can also open a good deal more than a file with CreateFile(), it is a very powerful API routine, unlike fopen() which is a C hangover.
Want a handle to the parallel port? CreateFile().
Need a pipe? CreateFile().
Mailslot perhaps? CreateFile().
fopen() is the part of the standard C-library, which is implemented using an OS dependent file I/O interface - this is CreateFile() on Window's machines.
Similiarly, open() is the OS interface for file I/O on most *nix OS's.
Thanks for the info. I had no idea CreateFile was so powerful.
I certainly appreciate your responses.