Reputation: 173
CreateFile
vs fopen
vs ofsteam
- advantage & disadvantage?
I heard that CreateFile powerful but only for windows.
Can you tell what should I use ( on windows) and why?
Upvotes: 12
Views: 10665
Reputation: 726
The obvious and clear winner is "CreateFile". Why?, because it has the most options. With it you have control over the buffering, can give it some hints for sequential or random access (needs profiling to see if there is any actual performance advantage), plus control over security and sharing.
It fits best within the context of the OP's question of "power".
If you need the extra control and options use "CreateFile", but probably most of the time you are fine with the ubiquitous tried and true "fopen". Or if you are big on C++ OOP mechanics, use "ofsteam".
Upvotes: 1
Reputation:
I copied my answer from
fopen or CreateFile in Windows
which was closed for some reason which escapes me...
Conclusions:
Don't be scared about these small tasks. You will actually know, what is happening in your code and these little pieces of code should be negligible (in amount of lines of code) compared to the code calling it. Also if you're using RAII for everything, the code calling into these utility classes, will be considerable less compared to when not using RAII and having to use two- or even more-step initialization and considerable less error prone. Replacing these utility classes with equivalent utility classes for other OS is also child play (using open()/close()/read()/write() on UNIXs).
And for the sake of the previous millennia don't read the google programming guidelines!
Upvotes: 1
Reputation:
If you want to use Windows file memory mapping you should use CreateFile
(e.g. the HANDLE
passed to CreateFileMapping API is the return value of CreateFile
). Moreover, CreateFile
offers higher customization options than C and C++ standard file API.
However, if you want to write portable code, or if you don't need Windows-specific features, C and C++ standard file APIs are just fine. In some tests, when processing large data, I noted some performance overhead of C++ I/O streams vs. raw C file API; if you happen to be in cases like this, you could simply wrap raw C file API in some C++ RAII class, and still use it in C++ code.
Upvotes: 3
Reputation: 4934
Unless you absoulutely need the extra functionality provided by OS API functions (like CreateFile
) I'd reccommend using the standard library functions (like fopen
or ofstream
). This way your program will be more portable.
The only real advantage of using CreateFile
that I can think of is overlapped I/O and maybe finer grained access rights.
Upvotes: 2
Reputation: 153909
It depends on what you're doing. For sequentially reading and writing text files, iostream is definitely the way to go. For anything involving transactional security or non-standard devices, you'll have to access the system directly (CreateFile
or open
). Even then, for sequential reading and writing of text, the best solution is to define your own streambuf
, and use that with iostream.
I can't think of any context where fopen
would be preferable.
Upvotes: 9
Reputation: 409166
Unless you need the features provided by the Windows file functions (e.g. overlapped I/O) then my suggestion is going with either iostreams in C++ or FILE
(fopen
and friends) in C.
Besides being more portable, you can also use formated input/output for text files, and for C++ it's easy to overload the output/input operators for your classes.
Upvotes: 4
Reputation: 21993
In most cases you will be better of using fopen in C or ofstream in C++. CreateFile gives some extra control over sharing and caching but does not provide formatting functionality.
Upvotes: 1