1

I am trying to create a file for handling session with in a directory name "IPM" i.e my project's name.

I access this file every time a user logged in and logged out plus I also access it at some more places thus i have created this function to create a path string so as to where the file is created on different OS

std::string SessionManager::createPathString(std::string sFileName)
{
   char* pPath = getenv(HOME);
   std::string sUserName(pPath);
   createDirectory(sUserName);

   std::replace(sUserName.begin(), sUserName.end(), '\\', '/');

   sUserName.append("/IPM");
   sUserName.append("/");
   sUserName.append(sFileName);

 return sUserName;
}

I call this function to get me the file path and the function to create directory goes like this

 int createDirectory(std::string sUserName)
{
   sUserName += "\\IPM";
 #ifdef _WIN32
   int ret = _mkdir(sUserName.c_str());
 #elif __linux__
   int ret = mkdir(sUserName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
 #endif
 return  ret;
}

It creates a directory on windows but fails on Linux, in case the directory or file is not present it gets created on windows, but not on Linux.

Is there any way to do it by boost, since I am new to C++ this look typical.

CMouse
  • 130
  • 3
  • 19
  • Why are you replacing the backslashes _after_ attempting to create the directory? – SingerOfTheFall Feb 09 '17 at 09:50
  • Windows uses backslashes and linux uses forwardslashes...http://stackoverflow.com/questions/38428561/difference-between-forward-slash-and-backslash-in-file-path – p0rter Feb 09 '17 at 09:51
  • Check your damned return value. – xaxxon Feb 09 '17 at 09:52
  • @CMouse May be, you should change the title to "Why does my file/dir manipulation works fine on Windows but not on Linux?" Otherwise, somebody will probably recommend you to read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)... – Scheff's Cat Feb 09 '17 at 09:57
  • Thanks @Scheff I'll do it – CMouse Feb 09 '17 at 09:58
  • @CMouse seriously. Why aren't you checking the return value from mkdir? – xaxxon Feb 09 '17 at 11:02
  • Yes I will once the basic code wokrs for me, which already did, so doing it now @xaxxon – CMouse Feb 09 '17 at 12:11
  • @CMouse the error code probably would have told you what was wrong in the first place. – xaxxon Feb 09 '17 at 12:32

1 Answers1

1

Yes, there is Boost.Filesystem library, that has create_directory function. You'd better use that, because it can handle different separators (like / vs \) more properly than just replacing chars in a strings from time to time.

To store path, you should then use boost::filesystem::path objects, that can created from char*, std::string or std::wstring strings, then append using operator /=, then call create_directory or any other method you need:

using namespace boost::filesystem;

path p(userName);
p /= "IPM"; // By the way, you should make this constant, don't you?
p /= sFileName;
if (!create_directory(p)) {
    cerr << "Failed to create directory";
}

More complete tutorial for Boost.Filesystem is available here.

Lapshin Dmitry
  • 1,084
  • 9
  • 28