Using fopen on a directory on windows - c++

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 Kits\10\Source\10.0.10586.0\ucrt\misc\errno.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.0\VC\crt\src\dosmap.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:\users\kaz>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.

Related

Visual studio does not see iostream, iomanip

I downloaded 'visual studio code' on a Macbook air (M2) and after running the program in C ++, visual studio gives an error 'iostream' file not found, 'iomanip' file not found. I think he does not see the standard libraries (it is not exactly known). Can you help please ? (By the way, in Xcode everything works fine).
[1]: https://i.stack.imgur.com/3xgAr.png
[2]: https://i.stack.imgur.com/KpCDO.png

Where are exception codes documented for microsoft applications?

I am running an application compiled with MSVC, I am experiencing an exception that is being caught and reported as Exception=0xc000008f. The application is written in C++;
Can someone help me find where these exception codes are documented?
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\excpt.h
The main place to look is the winerror.h file shipped in the SDK. (Usually found at c:\program files (x86)\Windows Kits\10\Include<version>\share\winerror.h). Sometimes the constants will be found in other files either also in shared or sometimes in um.
I tend to use a command line grep for performing such searches, though the Visual Studio "find in files" can also be made to search the include paths.
I will add that the exact error you ask about is called "ATUS_FLOAT_INEXACT_RESULT" and is defined in ntstatus.h rather than winerror.h

Command to Read file in CMake

Does anyone know of a way to read from a file in CMake, that works in Visual Studio? I have a cross-platform App. in Visual Studio 2017 that compiles with a toolchain in Linux, running in Hyper-V.
I have a simple defined version number in one of my H-files that I would like to read, parse, and append version number to the name of the compiled program (e.g., company_app_1.2.3).
I can't seem to get the file(READ) command to work. Visual Studio is using CMake version 3.11. I assume CMake can do this regardless of the g++ toolchain being used.
Command I tried:
#Read version
-file(READ FileNameHere $sValue)
Visual Studio Error I get:
1>/bin/sh: -c: line 0: syntax error near unexpected token `READ'
The MakeFile does not have a .txt extension, of follow Visual Studio's naming convention, but I would be willing to rename it if there was an advantage (intellisense available, or some more info about the error).
Thanks in advance for any replies.

Version Helper functions for Windows XP

In my application I need to check windows version. I'm using Version Helper functions in Visual Studio 2013 (v120) platform toolset. But then the program does not work in Windows XP. error is:
'xxx.exe' is not a valid win32 application
So I changed platform toolset to Visual Studio 2013 - Windows XP (v120_xp). But in this case application will not compile.
The error is:
error C1083: Cannot open include file: 'VersionHelpers.h': No such file or directory
What can I do? Can I use old GetVersionEx function? MSDN says:
GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead, use the Version Helper APIs
But may be I should use it for now!
I've managed to solve it using Remmy Lebeau's comment.
I copied VersionHelpers.h from C:\Program Files (x86)\Windows Kits\8.1\Include\um and winapifamily.h and sdkddkver.h from C:\Program Files (x86)\Windows Kits\8.1\Include\shared into my project and changed some includes in them.
Now it compiles with Windows XP toolset.
I've tested it and it's working as expected in all versions of windows.
How about this: try to link the functions manually with GetProcAddress to see what is available, If the new API is not, then the old must be, so you can use the one you managed to link without errors.
I am assuming you are building 32bit or 64bit and the OS in both cases is compatible.
EDIT:
GetVersionEx(), here is exported from kerner32.dll in XP, so you can try to link it using
GetProcAddress() here also has a suggestion by Microsft using "GetNativeSystemInfo".
The new API function
IsWindowsXPOrGreater() // also exported from kernel32.dll and ntdll.dll
can also be linked the same way using GetProcAddress(). This function will return an error if the function you are trying to link is not found, so you will know which one you have available. Then you call the available one and get the version info you need.
You only need to define the function prototypes yourself instead of using the headers, then define two function pointers, one for each, then assign them a NULL value initially until you try to link the functions. Then one pointer (or both) will end up with a value, and you can use the latest one available to call the linked API.

How to use Arduino with Microsoft Visual Studio ( Due to #include_next precompiler issues)

In short,
I need to do this,
#include_next <limits.h>
however Visual Studio doesn't like this and complains,
Error 40 error C1021: invalid preprocessor command 'include_next' c:\program files (x86)\arduino\hardware\tools\g++_arm_none_eabi\arm-none-eabi\include\limits.h 141 1 DataloggerFirmware (Visual Studio 2010)
I can't simply do
#include <limits.h>
there are too many files!!.
Error 9 error C1014: too many include files : depth = 1024 c:\program files (x86)\arduino\hardware\tools\g++_arm_none_eabi\arm-none-eabi\include\limits.h 141 1 DataloggerFirmware (Visual Studio 2010)
Is there any work around??
I figured it out. Posting here so maybe it helps someone else.
As I was most concerned with creating a Binary file for my Arduino board here are the steps.
Step 1
Make sure you have installed a version of the Arduino software from this link.
Step 2
Install the Visual Micro Utility. Follow this documentation carefully.
Step 3
Fire up Visual Studio, after installation is complete. Follow the steps it suggests. Choosing the appropriate folder for where you installed the Arduino Package.
Restart Visual Studio. You should now see a Arduino row of options added to your Visual Studio tab (under the File save etc options).
Select the board type and other options, and build. VoilĂ  you're done.
Special shoot out to the people in the comments section.
#include_next is a compiler extension (for example see Why would one use #include_next in a project?) and not part of the C++ language. As it's not part of the language your only real option (other than sticking with the original compiler that supports the extension) would be to restructure your code so that it doesn't use a user-defined file limits.h.