'How to create a folder with a dot in the name using std::filesystem?

There was a need to create a Windows directory with the name ".data". But when trying to create this path via std::filesystem:create_directory / create_directories, a folder is created in the directory above with an unclear name:

E:\n¬6Љ

P.S in the documentation for std::filesystem i found: dot: the file name consisting of a single dot character . is a directory name that refers to the current directory

My code:

ifstream Myfile;
string line;
char patht[MAX_PATH];
Myfile.open("dirs.lists");
if (Myfile.is_open())
{
    while (!Myfile.eof())
    {
        while (std::getline(Myfile, line))
        {
            sprintf(patht, "E:\\game\\%s", line);
            std::filesystem::create_directories(patht);
        }
    }
}

and dirs.lists contain:

game\\modloader\\.data
game\\modloader\\.profiles


Solution 1:[1]

You are passing a std::string to sprintf, you need to add c_str().

There is no reason to use sprintf in the first place, use std::filesystem::path:

#include <filesystem>
#include <fstream>
#include <string>

int main() {
  std::ifstream Myfile("dirs.lists");
  std::string line;
  std::filesystem::path root = "E:\\game";
  while (std::getline(Myfile, line))
  {
    std::filesystem::create_directories(root / line);
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1