<small id='Croek'></small><noframes id='Croek'>

      <bdo id='Croek'></bdo><ul id='Croek'></ul>

      <tfoot id='Croek'></tfoot>

    1. <legend id='Croek'><style id='Croek'><dir id='Croek'><q id='Croek'></q></dir></style></legend>

        <i id='Croek'><tr id='Croek'><dt id='Croek'><q id='Croek'><span id='Croek'><b id='Croek'><form id='Croek'><ins id='Croek'></ins><ul id='Croek'></ul><sub id='Croek'></sub></form><legend id='Croek'></legend><bdo id='Croek'><pre id='Croek'><center id='Croek'></center></pre></bdo></b><th id='Croek'></th></span></q></dt></tr></i><div id='Croek'><tfoot id='Croek'></tfoot><dl id='Croek'><fieldset id='Croek'></fieldset></dl></div>
      1. 从路径中获取文件名

        时间:2023-06-03

        <small id='6ChiP'></small><noframes id='6ChiP'>

          <bdo id='6ChiP'></bdo><ul id='6ChiP'></ul>
        • <tfoot id='6ChiP'></tfoot>

          <legend id='6ChiP'><style id='6ChiP'><dir id='6ChiP'><q id='6ChiP'></q></dir></style></legend>

          <i id='6ChiP'><tr id='6ChiP'><dt id='6ChiP'><q id='6ChiP'><span id='6ChiP'><b id='6ChiP'><form id='6ChiP'><ins id='6ChiP'></ins><ul id='6ChiP'></ul><sub id='6ChiP'></sub></form><legend id='6ChiP'></legend><bdo id='6ChiP'><pre id='6ChiP'><center id='6ChiP'></center></pre></bdo></b><th id='6ChiP'></th></span></q></dt></tr></i><div id='6ChiP'><tfoot id='6ChiP'></tfoot><dl id='6ChiP'><fieldset id='6ChiP'></fieldset></dl></div>

                    <tbody id='6ChiP'></tbody>
                • 本文介绍了从路径中获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从路径中获取文件名的最简单方法是什么?

                  What is the simplest way to get the file name that from a path?

                  string filename = "C:\MyDirectory\MyFile.bat"
                  

                  在这个例子中,我应该得到MyFile".无需扩展.

                  In this example, I should get "MyFile". without extension.

                  推荐答案

                  _splitpath 应该做你需要的.您当然可以手动完成,但 _splitpath 也可以处理所有特殊情况.

                  _splitpath should do what you need. You could of course do it manually but _splitpath handles all special cases as well.

                  正如 BillHoag 提到的,建议使用更安全的 _splitpath 版本,称为 _splitpath_s 可用时.

                  As BillHoag mentioned it is recommended to use the more safe version of _splitpath called _splitpath_s when available.

                  或者如果你想要一些便携的东西,你可以做这样的事情

                  Or if you want something portable you could just do something like this

                  std::vector<std::string> splitpath(
                    const std::string& str
                    , const std::set<char> delimiters)
                  {
                    std::vector<std::string> result;
                  
                    char const* pch = str.c_str();
                    char const* start = pch;
                    for(; *pch; ++pch)
                    {
                      if (delimiters.find(*pch) != delimiters.end())
                      {
                        if (start != pch)
                        {
                          std::string str(start, pch);
                          result.push_back(str);
                        }
                        else
                        {
                          result.push_back("");
                        }
                        start = pch + 1;
                      }
                    }
                    result.push_back(start);
                  
                    return result;
                  }
                  
                  ...
                  std::set<char> delims{'\'};
                  
                  std::vector<std::string> path = splitpath("C:\MyDirectory\MyFile.bat", delims);
                  cout << path.back() << endl;
                  

                  这篇关于从路径中获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用_CRT_SECURE_NO_WARNINGS 下一篇:为什么这个程序会崩溃:在 DLL 之间传递 std::string

                  相关文章

                  1. <i id='uGi4L'><tr id='uGi4L'><dt id='uGi4L'><q id='uGi4L'><span id='uGi4L'><b id='uGi4L'><form id='uGi4L'><ins id='uGi4L'></ins><ul id='uGi4L'></ul><sub id='uGi4L'></sub></form><legend id='uGi4L'></legend><bdo id='uGi4L'><pre id='uGi4L'><center id='uGi4L'></center></pre></bdo></b><th id='uGi4L'></th></span></q></dt></tr></i><div id='uGi4L'><tfoot id='uGi4L'></tfoot><dl id='uGi4L'><fieldset id='uGi4L'></fieldset></dl></div>

                    <legend id='uGi4L'><style id='uGi4L'><dir id='uGi4L'><q id='uGi4L'></q></dir></style></legend>

                      <small id='uGi4L'></small><noframes id='uGi4L'>

                    1. <tfoot id='uGi4L'></tfoot>

                        <bdo id='uGi4L'></bdo><ul id='uGi4L'></ul>