error using directory_entry in boost filesystem - boost

I'm starting to use the Boost library in my C++ programs using Code Blocks on Ubuntu.
I encounter a problem while manipulating files, the following code returns a segmentation fault :
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
int main()
{
boost::filesystem::path my_file("/home/malinou/workspace/grunbaum2/grunbaum/Bases/config.txt");
cout << "my_file path : " << my_file.string() << endl;
cout << "my_file exists : " << boost::filesystem::exists(my_file.string()) << endl;
cout << "my_file path : " << my_file.string() << endl;
return 0;
}
I'm using gcc compiler with flags -lboost_system and -lboost_filesystem, and the console output is :
my_file path : /home/malinou/workspace/grunbaum2/grunbaum/Bases/config.txt
my_file exists : 1
Segmentation fault (core dumped)
Process returned 139 (0x8B) execution time : 0.093 s
Press ENTER to continue.
Any idea why my_file seems to cause a problem after calling the exists() function?
(P.S: The problem is the same when I call the is_regular_file() function instead of the exists() one.)

To finally answer my question, it seemed to be a linking error due to the IDE I used. I created a new project with the same files, and it worked perfectly...weird
Anyway, thanks for everyones' help!

Related

C++ program terminating after std::bad_alloc when operator new is not used

according to cplusplus.com on std::bad_alloc
Type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space.
However, in my code the new operator is not used:
#include <iostream>
#include <boost/filesystem.hpp>
#include <cstdint>
using namespace std;
using namespace boost::filesystem;
int main()
{
path p{};
std::cin >> p;
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p)) // is p a directory?
cout << p << "is a directory\n";
else
cout << p << "exists, but is neither a regular file nor a directory\n";
}
else
cout << p << "does not exist\n";
return 0;
}
(code taken from boost filesystem tutorial)
and yet the console says:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)

Long path name in SetCurrentDirectoryW

Using this msdn reference I created a small use case.
#include <iostream>
#include <windows.h>
int main()
{
const std::wstring dir_path = L"\\\\?\\c:\\temp\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory\\ThisIsPrettyLongNameForDirectory";
if (!CreateDirectoryW(dir_path.c_str(), NULL))
{
std::cout << " Error occurred while creation of directory!!! " << GetLastError() << std::endl;
}
else
{
std::cout << "Lenth is " << dir_path.length() << std::endl;
if (dir_path.length() > 255)
{
if (!SetCurrentDirectoryW(buf.c_str()))
{
std::cout << " Error occurred!!! " << GetLastError() << std::endl;
}
else
{
std::cout << " Successful!!!" << std::endl;
}
}
}
return 0;
}
However, I get the output as
Error occurred while creation of directory!!! 3
Error code 3 points me to
ERROR_PATH_NOT_FOUND
3 (0x3)
The system cannot find the path specified.
Are there any other way to use long path names in CreateDirectoryW or SetCurrentDirectoryW APIs that I am unaware of ?
PS:I am using VS 2015 Update 3 compiler on Windows 7.
You have to create each directory on the path one by one. So the parent directory to the directory you pass must exist before you call CreateDirectory.
This information can be found by reading the documentation. It specifically calls out that error code and says:
One or more intermediate directories do not exist; this function will only create the final directory in the path.

SDL2: undefined references to strange functions

i have written this small piece of Code for testing purposes:
#include <iostream>
#include "SDL2/SDL.h"
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
// Betriebssystem ermitteln
std::string PlatFormString;
PlatFormString = SDL_GetPlatform();
std::cout << PlatFormString << "\n";
// Separator ermitteln
char Separator = '/';
if (PlatFormString == "Windows") {
Separator = '\\';
}
std::cout << "Separator: " << Separator << "\n";
// Installationspfad ermitteln
std::string InstallPath;
InstallPath = SDL_GetBasePath();
std::cout << InstallPath << "\n";
// Benutzerverzeichnis ermitteln
char* UserPath;
UserPath = SDL_GetPrefPath("TFF", "Blaster");
if (UserPath == nullptr) {
std::cout << "No Userpath aviable !! \n";
}
else {
std::cout << UserPath << "\n";
}
SDL_Quit();
return 0;
};
Under Linux eerthing works fine.
But under Windows, i am getting these strange errors ...
-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------
g++.exe -LD:\mingw64 -LD:\mingw64\bin -LD:\mingw64\include -LD:\mingw64\include\SDL2 -LD:\mingw64\lib -o bin\Debug\Test.exe obj\Debug\src\Test.o -lmingw32 -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer ..\..\mingw64\lib\libSDL2main.a ..\..\mingw64\lib\libSDL2.a
..\..\mingw64\lib\libSDL2.a(SDL_systimer.o): In function `timeSetPeriod':
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:58: undefined reference to `__imp_timeBeginPeriod'
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:52: undefined reference to `__imp_timeEndPeriod'
/Users/slouken/release/SDL/SDL2-2.0.3-source/foo-x64/../src/timer/windows/SDL_systimer.c:58: undefined reference to `__imp_timeBeginPeriod'
and so on. I dont know whats going on there. Can anyone help ?
I#m using Codeblocks 13.12, minGW64 (4.8.1), SDL 2.0.3 and Windows 7 64bit
You need to link against winmm.lib.
Try adding
#pragma comment(lib, "winmm.lib")
to your source.
I am posting this about a year later but for the future searchers here is the solution. Replace libSDL2.a with libSDL2.dll.a and it will compile just fine. The issue has something to do with dynamic and static linking with a windows machine or something I personally do I understand it completely but it works.
I came across the solution by reading this article: http://tech.yipp.ca/sdl/how-to-fix-libsdla-undefined-reference/
However this goes on a whole other solution I read between the lines or more particularly.
This is a really a rare problem that would occur only when you try to link with libSDL.a static library instead of the dynamic library SDL.dll. Then you have to add those library that SDL.dll normally links against which are the three above.

In Boost.Test, how to obtain the name of current test?

In Boost.Test, how can I obtain the name of the current auto test case?
Example:
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(MyTest)
{
std::cerr << "Starting " << test_name << std::endl;
// lots of code here
std::cerr << "Ending " << test_name << std::endl;
}
In the example, I want the variable test_name to contain "MyTest".
There is an undocumented* function that may be called for that purpose. The following line will flush the name of the current test to cerr:
#include <boost/test/framework.hpp>
...
std::cerr << boost::unit_test::framework::current_test_case().p_name
<< std::endl;
Note however that using this API does not flush the parameters in case of parametrized tests.
You might also be interested in the test checkpoints** (which seems to be what you want to do.)
#include <boost/test/included/unit_test.hpp>
...
BOOST_AUTO_TEST_CASE(MyTest)
{
BOOST_TEST_CHECKPOINT("Starting");
// lots of code here
BOOST_TEST_CHECKPOINT("Ending");
}
EDIT
* The current_test_case() function is now documented, see the official Boost documentation.
** BOOST_TEST_CHECKPOINT was previously called BOOST_CHECKPOINT. See the Boost changelog (1.35.0).
A different question about suite names gives a way to extract the name rather than just printing it:
auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)

GetModuleFileName does not return the path of the calling program

Why doesn't the following code print the path name of the calling program?
#include<iostream>
int main()
{
TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,sizeof(buffer));
cout << "Filepath:" << buffer << "\n";
}
Thanks everyone for the help, the problem was with cout, I used wprintf and it worked..
Could it be that you don't see the content of the buffer (because of the fact that the application dies) but the buffer actually contains the data you expects?

Resources