问题
I see these 3 functions are all related to opening a file.
open:
This POSIX function is deprecated. Use the ISO C++ conformant _open instead.
_open:
Opens a file. These functions are deprecated because more-secure versions are available; see _sopen_s, _wsopen_s.
fopen:
Opens a file. More-secure versions of these functions that perform additional parameter validation and return error codes are available; see fopen_s, _wfopen_s.
So, why there are three of them? When to use which? I thought POSIX is good but why MSDN says the POSIX version of open
is deprecated? And is there any naming convention related to the leading underscore so I can pick the right function based its first looking?
And when I am looking into the ACPICA code, I see below code:
It seems the _XXX
version can disable some MS language extensions, what exactly are these extensions?
/*
* Map low I/O functions for MS. This allows us to disable MS language
* extensions for maximum portability.
*/
#define open _open
#define read _read
#define write _write
#define close _close
#define stat _stat
#define fstat _fstat
#define mkdir _mkdir
#define snprintf _snprintf
#if _MSC_VER <= 1200 /* Versions below VC++ 6 */
#define vsnprintf _vsnprintf
#endif
#define O_RDONLY _O_RDONLY
#define O_BINARY _O_BINARY
#define O_CREAT _O_CREAT
#define O_WRONLY _O_WRONLY
#define O_TRUNC _O_TRUNC
#define S_IREAD _S_IREAD
#define S_IWRITE _S_IWRITE
#define S_IFDIR _S_IFDIR
ADD 1
It seems the single underscore prefix _XXX
is a Microsoft convention. Such as _DEBUG, _CrtSetDbgFlag, and the aforementioned _open. Some quote from the MSDN:
In Microsoft C++, identifiers with two leading underscores are reserved for compiler implementations. Therefore, the Microsoft convention is to precede Microsoft-specific keywords with double underscores. These words cannot be used as identifier names.
Microsoft extensions are enabled by default. To ensure that your programs are fully portable, you can disable Microsoft extensions by specifying the ANSI-compatible /Za command-line option (compile for ANSI compatibility) during compilation. When you do this, Microsoft-specific keywords are disabled.
When Microsoft extensions are enabled, you can use the Microsoft-specific keywords in your programs. For ANSI compliance, these keywords are prefaced by a double underscore. For backward compatibility, single-underscore versions of all the double-underscored keywords except __except, __finally, __leave, and __try are supported. In addition, __cdecl is available with no leading underscore.
The __asm keyword replaces C++ asm syntax. asm is reserved for compatibility with other C++ implementations, but not implemented. Use __asm.
The __based keyword has limited uses for 32-bit and 64-bit target compilations.
Though according to above quote, __int64
and _int64
should both work, but Visual Studio provide NO syntax highlight for _int64
. But _int64
can compile, too.
ADD 2
snprintf() and _snprintf()
回答1:
As far as Windows is concerned, the function for opening files is CreateFile. This returns a
HANDLE
and is provided by Kernel32.dll, not by Visual Studio. TheHANDLE
can be passed to other Windows API functions.The _open and open functions are POSIX compatibility functions to help you compile programs written for POSIX (Linux, macOS, BSD, Solaris, etc.) on Windows. These functions are defined by Visual Studio's C runtime, and presumably, they call
CreateFile
internally. The POSIX name of the function isopen
, but the function here is defined as_open
in case you have already defined a function namedopen
in your code. The function returns anint
which can be passed to other POSIX functions. On Windows, this interface is a compatibility API provided by Visual Studio, but on Linux and macOS, this interface is the direct interface for the operating system, just likeHANDLE
on Windows.The fopen function is part of the C standard. It is defined by Visual Studio's C runtime, and presumably, calls
CreateFile
internally. It returns aFILE *
which can be passed to other functions defined by the C standard.
So, to summarize the options:
If you need to use the Windows API directly, like calling
GetFileInformationByHandle
orCreateFileMapping
, you need aHANDLE
and you should probably callCreateFile
to open files.If you have a program which is already written for POSIX systems, then you can use
open
to make it easier to port your program to Windows. If you are only writing for Windows, there are no advantages to using this interface.If your program only needs to do basic file operations like opening, reading, and writing, then
fopen
is sufficient and it will also work on other systems. AFILE *
can be (and usually is) buffered by your application and supports convenient operations likefprintf
,fscanf
, andfgets
. If you want to callfgets
on a file returned byCreateFile
oropen
you will have to write it yourself.
It's possible to convert file handles from one API to the other, but you have to pay attention to ownership issues. "Ownership" is not really a technical concept, it just describes who is responsible for managing the state of an object, and you want to avoid destroying objects that you don't own, and avoid having multiple owners for the same object.
For the Windows API, you can use
_open_osfhandle()
to create aFILE *
from aHANDLE
, and_get_osfhandle()
to get theHANDLE
from theFILE *
. However, in both cases, the handle will be owned by theFILE *
.For the POSIX API, you can use
fdopen()
to create aFILE *
from aint
file descriptor, and you can usefileno()
to get theint
file descriptor from aFILE *
. Again, in both cases the file is owned by theFILE *
.
Note that portability is complicated by the fact that Windows filenames are arrays of wchar_t
, but macOS / Linux / etc. filenames are arrays of char
.
If you use a different C runtime like MinGW, or if you use the Windows Subsystem for Linux, things will be different.
来源:https://stackoverflow.com/questions/44792526/differenence-among-open-open-and-fopen-with-regard-to-msvc-compiler