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
CodePudding user response:
You are passing a std::string
to sprintf
, you need to add c_str()
but 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);
}
}
CodePudding user response:
You are passing the address of line
to sprintf
, not a string!
Try sprintf(patht, "E:\\game\\%s", line.c_str());