Visual C++ 2015 express: _stat not working on Windows XP - c++

Running the following example for _stat from MSDN compiled with Visual C++ 2015 Express using v140_xp as Platform Toolset (target Win32) runs normally on Windows 7 but not on Windows XP on several machines I tested.
// crt_stat.c
// This program uses the _stat function to
// report information about the file named crt_stat.c.
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main()
{
struct _stat buf;
int result;
char timebuf[26];
char* filename = "crt_stat.c"; // Absolute paths like "D:\\crt_stat.c" produce the same behaviour.
errno_t err;
// Get data associated with "crt_stat.c":
result = _stat( filename, &buf );
// Check if statistics are valid:
if ( result != 0 )
{
perror( "Problem getting information" );
switch ( errno )
{
case ENOENT:
printf( "File %s not found.\n", filename );
break;
case EINVAL:
printf( "Invalid parameter to _stat.\n" );
break;
default:
/* Should never be reached. */
printf( "Unexpected error in _stat.\n" );
}
}
else
{
// Output some of the statistics:
printf( "File size : %ld\n", buf.st_size );
printf( "Drive : %c:\n", buf.st_dev + 'A' );
err = ctime_s( timebuf, 26, &buf.st_mtime );
if ( err )
{
printf( "Invalid arguments to ctime_s." );
return 1;
}
printf( "Time modified : %s", timebuf );
}
}
Windows 7 output:
File size : 6
Drive : D:
Time modified : Tue Sep 8 10:06:57 2015
Windows XP output:
Problem getting information: Invalid argument
Invalid parameter to _stat.
And yes crt_stat.c is located in the executables directory which also is the CWD.
Is this a Bug or am I missing something?

As pointed out in the comments, it is a bug in the runtime. Right now (2015-09-09) the fix is not yet available in an update, but probably will be soon. A workaround is to use GetFileAttributesEx instead.

The bug is solved on Visual C++ Redistributable for Visual Studio 2015 Update 1
I solved the problem by installing that update form here: https://www.microsoft.com/en-us/download/details.aspx?id=49984

Related

How to search and move files from one path to another C++?

I'm trying to search all files that follow this pattern "console-*" and move them to another path on my machine if they exist. (For example from: /documents/fs/pstore to /sdk/sys/kernel_dump/).
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int dirExists(const char *pathname)
{
struct stat info;
if( stat( pathname, &info ) != 0 )
return 0; // printf( "cannot access %s\n", pathname );
else if( info.st_mode & S_IFDIR ) // S_ISDIR() doesn't exist on my windows
return 1; // printf( "%s is a directory\n", pathname );
else
return 0; // printf( "%s is no directory\n", pathname );
}
int main()
{
const char *path = "/documents/fs/pstore";
if(dirExists(path))
system("mv /documents/fs/pstore/console-* /sdk/sys/kernel_dump/ ");
else
printf( "error" );
return 0;
}
I've experienced with the code above, but it does not seem to work properly, should I try another approach, maybe with the rename() function? (I'm working on Linux)
Any advice would be greatly appreciated, thank you in advance.
You can call glob() and rename() C functions in Linux if C++17's std::filesystem is not an option for you. glob() gives you the list of files that matches globbing pattern like *, ? and []. However, rename() may fail if the mount position of from and to path are not identical. In this case, you should create your own copy and remove file function.

Redirecting standard output - Switched from VS2013 to VS2017, exception on writing

I've recently switched a project from using the VS2013 compiler to VS2017, and now the following code appears not to be working:
#include <windows.h>
#include <Fcntl.h>
#include <io.h>
#include <iostream>
#include <exception>
if( RedirectOutput )
{
//Setup stdout
HANDLE handle_stdout = GetStdHandle( STD_OUTPUT_HANDLE );
int fileDesc_stdout = _open_osfhandle( (long)handle_stdout, _O_TEXT );
if( fileDesc_stdout == -1 )
{
throw std::exception( "fileDesc_stdout is not valid" );
}
FILE* new_stdout = _fdopen( fileDesc_stdout, "w" );
if( !new_stdout )
{
throw std::exception( "new_stdout is not valid" );
}
FILE old_stdout = *stdout;
*stdout = *new_stdout;
std::cout.clear();
std::cout << "Output Redirected!\n";
}
This code is intended to redirect standard output to a console window, either the one that kicked off the current process or a console that is created via AllocConsole. (I've added the last line for testing purposes.)
The first time writing to cout occurs, the following exception is thrown (it otherwise doesn't write output and fails silently from then on):
Debug Assertion Failed!
Program: w:\build\MyApp.exe
File: minkernel\crts\ucrt\src\appcrt\stdio_flsbuf.cpp
Line: 26
Expression: ("inconsistent IOB fields", stream->_ptr - stream->_base >= 0)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
I can see that stdout is coming from corecrt_wstdio.h, but when I insert a breakpoint and add a watch, it says that the stdout is undefined, so I can't check the value.
Any ideas?
So I searched around a bit and found this post on SO.
Essentially, all of the code I posted can be replaced with the following:
freopen("CONOUT$", "w", stdout);
The first parameter of freopen is a filename, so I wonder if CONOUT$/CONIN$ represent an actual file, or if the function treats that input as a special case.

Making directory with vitual studio 2012 coding in c++

Good evening everyone, please I'm writing a Library management application in c++ using virtual studio 2012. I had already writing some codes using Dev c++ it worked but when I switched to visual studio it gives error. It involves creating folders and checking if the folders were actually created. That is using dir and mkdir.
Windows and Linux (POSIX) don't support the same API for most file system functions. You can use Microsoft's platform-specific APIs like CreateDirectory() or use the POSIX-like versions like _mkdir().
If you have a more recent C++ compiler / standard library, you can use the experimental filesystem library that is slated to become part of standard C++, perhaps as early as C++17. If not, you can use Boost.Filesystem from which the pre-standard experimental library was drawn.
Here's a complete, minimal example using Boost.Filesystem, which will work on both Windows and Linux without modification:
#include <iostream>
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
int main()
{
if( !fs::exists( "my_dir" ) )
{
if( fs::create_directory( "my_dir" ) )
{
std::cout << "Directory created!\n";
}
}
}
See it run: Coliru.
Here's the same code but with std::experimental::filesystem: Coliru.
You would need the appropriate include and linker paths setup in your build system for either of these to work locally. The biggest "gotcha" using the filesystem is that it throws exceptions for a lot of errors by default. You can either setup try/catch blocks at the appropriate places or pass in an error code param to make it return the status there instead.
#include <stdio.h>
#include <windows.h>
int main() {
if (!CreateDirectoryA("C:\\ABC123", NULL))
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("Already Exists");
}else if (GetLastError()== ERROR_PATH_NOT_FOUND)
{
printf("Path not found");
}
}else{
printf("Created..");
}
}
simple function will do.
Thanks alot guys but I found this that solved my problem
#include <iostream>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
int a;
cin >> a;
}
the cin >> a; is just to keep the output screen so I can see the result

C++ Visual Studio Current Working Directory

I have a similar problem to this:
Current working directory is visual studio directory
Except that I am working with a C++ project in Visual Studio. Any suggestions?
For example if I try the solution in the following post:
GetCurrentDirectory for startup App. c++
I get this:
"C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE"
But I want it to be the Debug folder under my project/solution folders.
Using the _fullpath command allowed me to extract the current directory. For example you can modify the example code on the linked page :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>
void PrintFullPath( char * partialPath )
{
char full[_MAX_PATH];
if( _fullpath( full, partialPath, _MAX_PATH ) != NULL )
printf( "Full path is: %s\n", full );
else
printf( "Invalid path\n" );
}
int main( void )
{
// Get current directory
PrintFullPath( ".\\" );
}

Unable to find source of exception: cudaError_enum at memory location

I am trying to identify the source of an Microsoft C++ exception:
First-chance exception at 0x770ab9bc in test_fft.exe: Microsoft C++ exception: cudaError_enum at memory location 0x016cf234...
My build environment is:
IDE: Microsoft Visual C++ 2010 Express
NVIDIA Driver: 301.27
CUDA: NVIDIA CUDA Toolkit v4.2 (32-bit)
SDK: NVIDIA GPU Computing SDK 4.2 (32-bit)
Problem scope: I am trying to wrap the CUFFT behind a C++ class. This way I can hide the translation from one data type to the cufftComplex, execution of the FFT and memory transfers from the calling code.
Class header:
#ifndef SIGNAL_PROCESSING_FFT_HPP
#define SIGNAL_PROCESSING_FFT_HPP
#include "signal_processing\types.hpp"
#include <boost/cstdint.hpp>
#include <cufft.h>
#include <vector>
namespace signal_processing {
class FFT {
public:
FFT ( boost::uint32_t size );
virtual ~FFT();
void forward ( ComplexVectorT const& input, ComplexVectorT& output );
void reverse ( ComplexVectorT const& input, ComplexVectorT& output );
private:
cufftComplex* m_device_data;
cufftComplex* m_host_data;
cufftHandle m_plan;
boost::uint32_t m_size;
};
}
#endif // SIGNAL_PROCESSING_FFT_HPP
FFT constructor:
FFT::FFT ( boost::uint32_t size )
: m_size ( size )
{
CudaSafeCall ( cudaMalloc((void**)&m_device_data, sizeof(cufftComplex) * m_size ) );
m_host_data = (cufftComplex*) malloc ( m_size * sizeof(cufftComplex) );
CufftSafeCall ( cufftPlan1d ( &m_plan, m_size, CUFFT_C2C, 1 ) );
}
The Microsoft C++ exception is being thrown in the FFT constructor at the first line where the call to cudaMalloc. This error only seems to occur if I run the code using the FFT class with the Visual Studio debugger.
References
CudaSafeCall definition
#define CudaSafeCall(err) __cudaSafeCall ( err, __FILE__, __LINE__ )
__cudaSafeCall definition
inline void __cudaSafeCall ( cudaError err, const char* file, const int line )
{
#ifdef CUDA_ERROR_CHECK
if ( cudaSuccess != err )
{
std::cerr << boost::format ( "cudaSafeCall() failed at %1$s:%2$i : %3$s\n" )
% file
% line
% cudaGetErrorString ( err );
exit(-1);
}
#endif
return;
}
The observation you are making has to do with an exception that is caught and handled properly within the CUDA libraries. It is, in some cases, a normal part of CUDA GPU operation. I believe your application is returning no API errors in this case. If you were not within the VS environment that can report this, you would not observe this at all.
This is considered normal behavior under CUDA. I believe there were some attempts to eliminate it in CUDA 5.5. You might wish to try that, although it's not considered an issue either way.