How do I recursively create a folder in Win32?

前端 未结 14 1311
孤城傲影
孤城傲影 2020-12-01 14:11

I\'m trying to create a function that takes the name of a directory (C:\\foo\\bar, or ..\\foo\\bar\\..\\baz, or \\\\someserver\\foo\\bar

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 14:31

    From http://www.cplusplus.com/reference/string/string/find_last_of/:

    // string::find_last_of
    #include 
    #include 
    using namespace std;
    
    void SplitFilename (const string& str)
    {
      size_t found;
      cout << "Splitting: " << str << endl;
      found=str.find_last_of("/\\");
      cout << " folder: " << str.substr(0,found) << endl;
      cout << " file: " << str.substr(found+1) << endl;
    }
    
    int main ()
    {
      string str1 ("/usr/bin/man");
      string str2 ("c:\\windows\\winhelp.exe");
    
      SplitFilename (str1);
      SplitFilename (str2);
    
      return 0;
    

    That should give you an idea on how to deal with the path string. Then after that, all you need to do is loop through the paths starting from the drive down to the deepest folder. Check if the folder exists, and if it doesn't, create it.

提交回复
热议问题