Using fopen on a directory on windows
I am trying to get the correct error code out of errno when opening a directory as a file, both with fopen as well as with ifstream.open(). I expect EISDIR, I get EACCES.
I am compiling (and executing) with the MSVC 12.0 toolchain on Windows 7 x64.
I have been reading in article (https://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/) where the author got the output "stream failbit (or badbit). error state: Is a directory".
I compiled the authors files with GCC 4.6 (or above, not sure atm) and on passing a directory as argument I get EACCES as well.
I know there is no easy way of telling whether a disk object is a directory on windows, so not getting EISDIR is not too surprising.
Is there anything that can be done about it (getting EISDIR on windows, that is)? Are there other errno's that behave in a similar (unexpected) way?
Microsoft's C runtime libraries define, but do not use the EISDIR
symbol. So you won't get that error code from them. To get the answer to your other question, you need to look in the C library source code. It ships with Visual Studio and, in case of Visual Studio 2015 and later, Windows SDK.
In Visual Studio 2015 (14.0), which uses the Universal CRT, the file you want is called errno.cpp
and it's included in the Windows SDK, I have it in c:Program Files (x86)Windows Kits10Source10.0.10586.0ucrtmiscerrno.cpp
.
In Visual Studio 2013 (12.0), the file you want is called dosmap.c
and it's included in the VC subdirectory of the Visual Studio installation directory, I have it in C:Program Files (x86)Microsoft Visual Studio 12.0VCcrtsrcdosmap.c
.
Both of those files contain an error table mapping OS error codes to C library error codes. You can use it to confirm whether a particular mapping conforms to your expectations.
The fopen
function isn't a part of the Windows API; it comes from the run-time support library from some given C or C++ development environment.
This EISDIR
error works in a C application built for Cygwin and shipped with Cygnal:
c:userskaz>txr
This is the TXR Lisp interactive listener of TXR 148.
Use the :quit command or type Ctrl-D on empty line to exit.
1> (open-file ".")
#<file-stream . 6fffff00738>
2> (get-string *1)
** error reading #<file-stream . 6fffff00738>: 21/"Is a directory"
** during evaluation at expr-2:1 of form (get-string *1)
3>
The open-file
function in this language uses fopen
, and get-string
ultimately relies on C stdio.h
functions. The fopen
succeeds, but the subsequent input attempt receives the EISDIR
error. (Mapped to the same traditional 21 code as on Linux and other platforms). That is turned into an exception. The "Is a directory"
string is from strerror
.
You just need a more richly featured C run-time library with better POSIX support than the paltry offering provided with Microsoft Visual Studio.
链接地址: http://www.djcxy.com/p/61310.html上一篇: 在Wpf中创建一个垂直菜单
下一篇: 在Windows上使用fopen目录