In Windows, should I use CreateFile or fopen, portability aside?
Asked Answered
B

4

10

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

Barnie answered 14/6, 2010 at 14:58 Comment(0)
C
10

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

CreateFile on the other side can be used not only for operations with files, but also with directories (with use of corresponding options), pipes and various Windows devices.

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

You can use CreateFile with a filename longer that MAX_PATH characters. It can be important for some server applications or ones which must be able to open any file (a virus scanner or a backup application for example). This is enabled by using namespace semantics, though this mode has its own concerns, like ability to actually create a file named ".." or L"\xfeff\x20\xd9ab" (good luck trying to delete them later).

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 open any file also a file to which you have no access through security descriptor.

If you only want to read the content of a file, you can use CreateFile, CreateFileMapping and MapViewOfFile to create file mapping. Then you can work with a file as with a block of memory, which can possibly increase your application's speed.

There are also other uses of the function, which are described in detail in the corresponding MSDN article.

So I can summarize: only if you have a hard portability requirements or if you need to pass a FILE* to some external library, then you have to use fopen. In all other cases I would recommend you to use CreateFile. For best results, I would also advise to learn Windows API specifically, as there are many features that you can find a good use for.

UPDATED: Not directly related to your question, but I also recommend you to take a glance at transactional I/O functions which are supported starting with Windows Vista. Using this feature, you can commit a bunch of operation with files, directories or registry as one transaction that cannot be interrupted. It is a very powerful and interesting tool. If you are not ready now to use the transactional I/O functions, you can start with CreateFile and port your application to transactional I/O later.

Clougher answered 14/6, 2010 at 16:27 Comment(0)
C
5

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.

Cloe answered 14/6, 2010 at 15:0 Comment(0)
M
1

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)

Marrymars answered 14/6, 2010 at 15:8 Comment(0)
T
0

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.

Tallyman answered 14/6, 2010 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.