【问题标题】:Is there a more efficient way to extract the file name of a file?有没有更有效的方法来提取文件的文件名?
【发布时间】:2022-01-25 22:19:14
【问题描述】:

下面的代码使用文件的路径提取文件的文件名。

#include <iostream>
#include <vector>

using namespace std;

int main() {
    cout << "Program operating..." << endl;

    string s = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1";
    string name;
    for (unsigned int i = s.size() - 1; i > 0; i--) {
        if (s[i] == '\\') {
            for (unsigned int j = i + 1; j < s.size(); j++) {
                if (s[j] == '.' || j == s.size() - 1) {
                    if (j == s.size() - 1)
                        j++;
                    vector<char> v(j - i - 1);
                    unsigned int l = 0;
                    for (unsigned int k = i + 1; k < j; k++) {
                        v[l++] = s[k];
                    }
                    char* vectorAsArray = &v[0];
                    name = vectorAsArray;
                    name.resize(v.size());
                    break;
                }
            }
            break;
        }
    }
    cout << name << endl;
}
#include <iostream>
#include <vector>

using namespace std;

int main() {
    cout << "Program operating..." << endl;

    string s = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1.png";
    string name;
    for (unsigned int i = s.size() - 1; i > 0; i--) {
        if (s[i] == '\\') {
            for (unsigned int j = i + 1; j < s.size(); j++) {
                if (s[j] == '.' || j == s.size() - 1) {
                    if (j == s.size() - 1)
                        j++;
                    vector<char> v(j - i - 1);
                    unsigned int l = 0;
                    for (unsigned int k = i + 1; k < j; k++) {
                        v[l++] = s[k];
                    }
                    char* vectorAsArray = &v[0];
                    name = vectorAsArray;
                    name.resize(v.size());
                    break;
                }
            }
            break;
        }
    }
    cout << name << endl;
}

有没有更有效的方法来做到这一点?

目的: 我正在制作一个纹理类,我想通过它的名称而不是它的路径或组成的 ID 来引用纹理。

注意:库根本不适用于 Visual Studio 编译器。如果您知道如何修复它或有替代解决方案,请发布。

【问题讨论】:

  • 你考虑过使用文件系统头吗?在 VS2019 和 2022 中功能齐全
  • 您必须通过以下方式将语言设置设置为使用 C++17 或更高版本:项目 > 属性 > C/C++ > 语言 > C++ 语言标准 > C++17、C++20 或预览版C++ 最新

标签: c++ file path filenames visual-studio-2022


【解决方案1】:

使用std::filesystem::path::filename

也就是说,您应该通过其完全限定路径或唯一 ID 来引用纹理;否则,不同文件夹中的同名纹理会发生冲突。

【讨论】:

  • 没有。此解决方案不起作用,因为 std::filesystem::path::filename 甚至无法被 Visual Studio 识别。否则我会使用它。另外,我想通过它的名称而不是它的路径来引用它的原因是因为当我尝试包含它时找不到文件系统的头文件。我在网上查看了许多示例,但我在此标题上找到的所有内容均无效。我不是唯一一个遇到这个问题的人。
  • 您使用的是什么版本的 Visual Studio? You may need to instruct VS to use C++17.
  • 如果 C++17 真的不可用,give this a try
  • 我使用的是 Visual Studio 2022。Visual Studio 2019 也有同样的问题。你在做什么不同的文件系统对你有用但对我不起作用?
  • 你提到了一些我回答了很多问题的事情。 C++ 的版本。显然,Visual Studio 默认使用 C++ 版本 14。为了能够访问文件系统库的所有功能,必须进入解决方案属性并更改 C++ 版本。太傻了。
【解决方案2】:

最简单的解决方案是使用 C++17 以后的 &lt;filesystem&gt; 库,特别是 std::filesystem::path 类及其 stem() 方法:

返回由去掉其扩展名的通用格式路径标识的文件名。

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;
using namespace std;

int main() {
    cout << "Program operating..." << endl;

    //fs::path p = "C:" / "Users" / "user" / "Pictures" / "Strategic_Offense_Logo_1.png";
    fs::path p = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1.png";
    fs::path name = p.stem();

    cout << name << endl;
}

但是,如果这不是一个选项,使用std::string 的可用方法有一个简单的解决方案,例如rfind()substr()

#include <iostream>
#include <string>

using namespace std;

int main() {
    cout << "Program operating..." << endl;

    string s = "C:\\Users\\user\\Pictures\\Strategic_Offense_Logo_1.png";

    string name = s.substr(s.rfind('\\') /* or: s.find_last_of("\\/") */ + 1);
    name.resize(name.rfind('.'));

    cout << name << endl;
}

【讨论】:

    【解决方案3】:

    Visual Studio 默认为 C++14。为了能够访问&lt;filesystem&gt; 及其所有功能(如提取文件名),必须进入 C++ 下的解决方案属性并将版本更改为最新。然后,也只有这样,&lt;filesystem&gt; 才会起作用,正如 Internet 和 StackOverflow 中的每个人都在谈论的那样。

    【讨论】:

    • Pre C++17 boost::filesystem 提供了与 C++17 之前的std::filesystem 兼容的几乎。实际上,它是std::filesystem boost 提出的,但经过一些调整,进入了std
    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2011-03-24
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多