views:

92

answers:

4

What are the differences, and in what cases one or the other would prove superior in some way?

+3  A: 

That really depends on what type of program you are writing. If it is supposed to be portable, fopen will make your life easier. fopen will call CreateFile "behind the scenes".

Some more advanced options (cache control, file access control, etc) are only available if you are using the Win32 API (they depend on the Win32 file handle, as opposed to the FILE pointer in stdio), so if you are writing a pure Win32 application, you may want to use CreateFile.

Krumelur
+1  A: 

CreateFile lets you

  • Open file for asynchronous I/O
  • Pass optimization hints like FILE_FLAG_SEQUENTIAL_SCAN
  • Set security and inherit settings without threading issues

They don't return the same handle type, with fopen/FILE object you can call other runtime functions such as fputs (as well as converting it to a "native" file handle)

Anders
+2  A: 

First of all the function fopen can be used only for a simple portable operation with files.

CreateFile on the other side can be used not only with operation with files, but for with directories (is you use corresponding options), pipes and communication with different Windows devices.

CreateFile has a lot of additional useful switches like FILE_FLAG_NO_BUFFERING or FILE_ATTRIBUTE_TEMPORARY, FILE_FLAG_SEQUENTIAL_SCAN and so on which can be very useful in different scenarios.

You can use CreateFile with a filename longer as MAX_PATH characters. It can be important for some application running on server or which must be able to open any file (a virus scanner or a backup application for example). To make so you should just start the path with "\\?\" characters.

You can use CreateFile in different security scenarios. I mean not only usage of security attributes. If current process has SE_BACKUP_NAME or SE_RESTORE_NAME privilege (like Administrators typically have) and enable this privilege, one can use CreateFile to onen any file also a file to which you have no access through security descriptor.

If you only want to read contain from the file. You can use CreateFile, CreateFileMapping and MapViewOfFile to create file mapping. Then you can work with the file as with the block of memory. With the way you can reach the better performance in input/output operation with the files under Windows.

I can continue the list of features of CreateFile function, but I want not spend your time.

So I can summarize: only if you have a hard portability requirements or if you really need to give a FILE * handle to another functions which you must use you should use fopen. In all other cases I would recommend you to use CreateFile. To receive the best results you should study the reach possibilities which you have with CreateFile.

UPDATED: It is not direct your question, but I recommend you also take a glance at transactional I/O functions (see http://msdn.microsoft.com/en-us/library/cc303705.aspx) which are supported starting with Windows Vista. One from there is CreateFileTransacted function, which is almost the same as CreateFile. With respect of this functions you can commit some operations with files, directories or registry as one transaction. It is a very powerful and interesting possibility. If you not ready now to use this transactional I/O functions, you can start with CreateFile and port your application to transactional I/O later.

Oleg
A: 

Whenever possible, prefer object oriented wrappers that support RAII, like fstream or boost file IO objects.

You should, of course, care about the share mode, so fopen() and STL are insufficient.

Pavel Radzivilovsky