【问题标题】:Get parent directory from file in C++从 C++ 文件中获取父目录
【发布时间】:2011-08-08 08:46:10
【问题描述】:

我需要在 C++ 中从文件中获取父目录:

例如:

输入:

D:\Devs\Test\sprite.png

输出:

D:\Devs\Test\ [or D:\Devs\Test]

我可以用一个函数来做到这一点:

char *str = "D:\\Devs\\Test\\sprite.png";
for(int i = strlen(str) - 1; i>0; --i)
{
    if( str[i] == '\\' )
    {
        str[i] = '\0';
        break;
    }
}

但是,我只想知道存在一个内置函数。 我用的是 VC++ 2003。

提前致谢。

【问题讨论】:

    标签: c++ visual-c++ dev-c++


    【解决方案1】:

    如果您使用 std::string 而不是 C 样式的 char 数组,则可以按以下方式使用 string::find_last_ofstring::substr

    std::string str = "D:\\Devs\\Test\\sprite.png";
    str = str.substr(0, str.find_last_of("/\\"));
    

    【讨论】:

    • 我喜欢这个答案,但我认为如果找不到目录分隔符,则需要返回 string::npos 的地址。因此,如果找到目录分隔符,那么应该是父目录路径最终成为文件路径。我认为应该检查string::npos,如果没有找到分隔符,则返回.
    【解决方案2】:

    现在,C++17 可以使用std::filesystem::path::parent_path

        #include <filesystem>
        namespace fs = std::filesystem;
    
        int main() {
            fs::path p = "D:\\Devs\\Test\\sprite.png";
            std::cout << "parent of " << p << " is " << p.parent_path() << std::endl;
            // parent of "D:\\Devs\\Test\\sprite.png" is "D:\\Devs\\Test"
    
            std::string as_string = p.parent_path().string();
            return 0;
        }
    

    【讨论】:

      【解决方案3】:

      重载和跨平台方式是使用boost::filesystem::parent_path()。但显然这会增加您可能不希望的开销。

      或者,您可以使用 cstring 的 strrchr 函数,如下所示:

      include <cstring>
      char * lastSlash = strrchr( str, '\\');
      if ( *lastSlash != '\n') *(lastSlash +1) = '\n';
      

      【讨论】:

        【解决方案4】:

        编辑 const 字符串是未定义的行为,因此声明如下:

        char str[] = "D:\\Devs\\Test\\sprite.png";
        

        您可以使用以下 1 条衬里来获得您想要的结果:

        *(strrchr(str, '\\') + 1) = 0; // put extra NULL check before if path can have 0 '\' also
        

        【讨论】:

          【解决方案5】:

          在符合 POSIX 的系统 (*nix) 上,此dirname(3) 有一个常用功能。在windows上有_splitpath

          _splitpath 函数断开路径 分为四个部分。

          void _splitpath(
             const char *path,
             char *drive,
             char *dir,
             char *fname,
             char *ext 
          );
          

          所以结果(我认为您正在寻找的结果)将在dir 中。

          这是一个例子:

          int main()
          {
              char *path = "c:\\that\\rainy\\day";
              char dir[256];
              char drive[8];
              errno_t rc;
          
          
              rc = _splitpath_s(
                  path,       /* the path */
                  drive,      /* drive */
                  8,          /* drive buffer size */
                  dir,        /* dir buffer */
                  256,        /* dir buffer size */
                  NULL,       /* filename */
                  0,          /* filename size */
                  NULL,       /* extension */
                  0           /* extension size */
              );
          
              if (rc != 0) {
                  cerr << GetLastError();
                  exit (EXIT_FAILURE);
              }
          
              cout << drive << dir << endl;
              return EXIT_SUCCESS;
          }
          

          【讨论】:

            【解决方案6】:

            在 Windows 平台上,您可以使用 PathRemoveFileSpecPathCchRemoveFileSpec 为了达成这个。 但是为了便携性,我会采用此处建议的其他方法。

            【讨论】:

              【解决方案7】:

              你可以使用 dirname 来获取父目录 查看此link 了解更多信息

              拉古

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-08-14
                • 2017-07-24
                • 1970-01-01
                • 1970-01-01
                • 2017-05-04
                • 1970-01-01
                • 2017-04-03
                相关资源
                最近更新 更多