在 Windows 中,除了可移植性之外,我应该使用 CreateFile 还是 fopen?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3038255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Windows, should I use CreateFile or fopen, portability aside?
提问by CannibalSmith
What are the differences, and in what cases one or the other would prove superior in some way?
有什么区别,在什么情况下,一个或另一个会以某种方式证明是优越的?
采纳答案by Oleg
First of all the function fopen
can be used only for simple portable operations with files.
首先,该功能fopen
只能用于对文件进行简单的便携式操作。
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
另一方面不仅可以用于对文件的操作,还可以用于目录(使用相应的选项)、管道和各种 Windows 设备。
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.
CreateFile
有很多额外的有用开关,比如FILE_FLAG_NO_BUFFERING
,FILE_ATTRIBUTE_TEMPORARY
和FILE_FLAG_SEQUENTIAL_SCAN
,它们在不同的场景中非常有用。
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 anyfile (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).
您可以使用比字符CreateFile
长的文件名MAX_PATH
。对于某些服务器应用程序或必须能够打开任何文件的服务器应用程序(例如病毒扫描程序或备份应用程序)来说,这可能很重要。这是通过使用命名空间语义来实现的,尽管这种模式有其自身的问题,例如能够实际创建一个名为".."
or的文件L"\xfeff\x20\xd9ab"
(祝你以后尝试删除它们好运)。
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 filealso a file to which you have no access through security descriptor.
您可以CreateFile
在不同的安全场景中使用。我的意思不仅是使用安全属性。如果当前进程具有 SE_BACKUP_NAME 或 SE_RESTORE_NAME 权限(如管理员通常拥有的权限)并启用此权限,则可以使用CreateFile
打开任何文件以及您无法通过安全描述符访问的文件。
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.
如果只想读取文件的内容,可以使用CreateFile
,CreateFileMapping
和MapViewOfFile
创建文件映射。然后,您可以像处理内存块一样处理文件,这可能会提高应用程序的速度。
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.
所以我可以总结一下:只有当你有硬性的可移植性要求或者你需要将 a 传递FILE*
给某个外部库时,你才必须使用fopen
. 在所有其他情况下,我建议您使用CreateFile
. 为了获得最佳效果,我还建议您专门学习 Windows API,因为您可以找到许多有用的功能。
UPDATED: Not directly related to your question, but I also recommend you to take a glance at transactional I/Ofunctions 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.
更新:与您的问题没有直接关系,但我还建议您看一下从 Windows Vista 开始支持的事务 I/O函数。使用此功能,您可以将一系列文件、目录或注册表操作提交为一个不可中断的事务。这是一个非常强大和有趣的工具。如果您现在还没有准备好使用事务 I/O 功能,您可以CreateFile
稍后开始并将您的应用程序移植到事务 I/O。
回答by Krumelur
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".
这实际上取决于您正在编写的程序类型。如果它应该是便携式的,fopen
将使您的生活更轻松。fopen
将称为CreateFile
“幕后”。
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.
一些更高级的选项(缓存控制、文件访问控制等)仅在您使用 Win32 API 时可用(它们依赖于 Win32 文件句柄,而不是FILE
stdio 中的指针),因此如果您正在编写纯 Win32应用程序,您可能想要使用 CreateFile。
回答by Anders
CreateFile lets you
CreateFile 让你
- Open file for asynchronous I/O
- Pass optimization hints like FILE_FLAG_SEQUENTIAL_SCAN
- Set security and inherit settings without threading issues
- 为异步 I/O 打开文件
- 传递优化提示,如 FILE_FLAG_SEQUENTIAL_SCAN
- 在没有线程问题的情况下设置安全性和继承设置
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)
它们不返回相同的句柄类型,使用 fopen/FILE 对象,您可以调用其他运行时函数,例如 fputs(以及将其转换为“本机”文件句柄)
回答by Pavel Radzivilovsky
Whenever possible, prefer object oriented wrappers that support RAII, like fstream or boost file IO objects.
尽可能选择支持 RAII 的面向对象的包装器,例如 fstream 或 boost 文件 IO 对象。
You should, of course, care about the share mode, so fopen() and STL are insufficient.
当然,你应该关心共享模式,所以 fopen() 和 STL 是不够的。