winapi - In Windows, should I use CreateFile or fopen, portability aside? -
What are the differences, and in which case would one prove better than one or the other? The
First of all function fopen
using simple portable works only with files Can be done for.
CreateFile
on the other hand can be used not only for the operation of files, but also for directories (with the use of this option), pipes and various Windows devices.
CreateFile
a lot of additional useful list, FILE_FLAG_NO_BUFFERING
, FILE_FLAG_SEQUENTIAL_SCAN
That can be very useful, in different circumstances.
You can use the CreateFile
with a filename of MAX_PATH
characters for a long time. This may be important for some server applications or those who should be able to open any any file (for example a virus scanner or backup application). This name space is enabled using cementics, though this mode has its own concern, such as ".."
or L "\ xfeff \ x20 \ xd9ab"
(Good luck trying to remove them later).
In various security scenarios CreateFile can use
I mean not only SE_BACKUP_NAME or SE_RESTORE_NAME the current process that uses security features privileges (like administrators usually) and If you enable this privilege, then any file can also use CreateFile
to open a file which you do not have access to
CreateFile
, CreateFileMapping
and MapViewOfFile
to create the file mapping the block of memory with a file Which may potentially speed up your application. There are other uses of the function, which details have been described in detail
So I can summarize: Only if you have hard portability requirements or some external Library FILE * need to have
, you fopen
will be used in all other cases, I advise you to use CreateFile
Give. For best results, I especially recommend learning the Windows API, because there are many features for which you can get good use.
Upgrade : Not directly related to your question, but I also recommend taking a look at the tasks that are supported with Windows Vista. Using the feature, you can do a bunch of operations with files, directories or registry as a transaction that can not be interrupted. This is a very powerful and interesting tool if you are no longer ready to use transaction I / O functions, then you can start with CreateFile
and later your application to transaction I / O Can close
Comments
Post a Comment