【问题标题】:c++ renaming filename other languagec++重命名文件名其他语言
【发布时间】:2019-12-21 11:51:29
【问题描述】:

我想重命名一些文件,

部分文件的名称为俄文、中文和德文

程序只能修改英文名称的文件。

有什么问题?请指导我

std::wstring ToUtf16(std::string str)
{
    std::wstring ret;
    int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
    if (len > 0)
    {
        ret.resize(len);
        MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &ret[0], len);
    }
    return ret;
}
int main()
{
    const std::filesystem::directory_options options = (
        std::filesystem::directory_options::follow_directory_symlink |
        std::filesystem::directory_options::skip_permission_denied
        );
    try
    {
        for (const auto& dirEntry :
            std::filesystem::recursive_directory_iterator("C:\\folder",
                std::filesystem::directory_options(options)))
        {
            filesystem::path  myfile(dirEntry.path().u8string());
            string uft8path1 = dirEntry.path().u8string();
            string uft8path3 = myfile.parent_path().u8string() + "/" + myfile.filename().u8string();
            _wrename(
                ToUtf16(uft8path1).c_str()
                ,
                ToUtf16(uft8path3).c_str()
            );
            std::cout << dirEntry.path().u8string() << std::endl;
        }
    }
    catch (std::filesystem::filesystem_error & fse)
    {
        std::cout << fse.what() << std::endl;
    }
    system("pause");
}

【问题讨论】:

  • 你试过使用 filesystem::rename 吗?

标签: c++ unicode file-rename


【解决方案1】:
filesystem::path  myfile(dirEntry.path().u8string());

Windows 支持 UTF16 和 ANSI,API 不支持 UTF8(无论如何都不是标准的)。当您提供 UTF8 字符串时,它认为有 ANSI 输入。使用wstring()表示UTF16:

filesystem::path  myfile(dirEntry.path().wstring());

或者直接说:

filesystem::path  myfile(dirEntry);

同样,将wstring() 用于其他对象。

wstring path1 = dirEntry.path();
wstring path3 = myfile.parent_path().wstring() + L"/" + myfile.filename().wstring();
_wrename(path1.c_str(), path3.c_str());

当您有 UTF16 输入时,重命名文件将正常工作。但是控制台有限的 Unicode 支持还有另一个问题。您无法通过字体更改打印某些亚洲字符。使用调试器或MessageBoxW 查看亚洲字符。

使用_setmodewcout 打印UTF16。

另请注意,std::filesystem 支持 / 运算符添加路径。示例:

#include <io.h> //for _setmode
#include <fcntl.h> 
...

int main()
{
    _setmode(_fileno(stdout), _O_U16TEXT);

    const std::filesystem::directory_options options = (
        std::filesystem::directory_options::follow_directory_symlink |
        std::filesystem::directory_options::skip_permission_denied
        );
    try
    {
        for(const auto& dirEntry :
            std::filesystem::recursive_directory_iterator(L"C:\\folder",
                std::filesystem::directory_options(options)))
        {
            filesystem::path  myfile(dirEntry);
            auto path1 = dirEntry;
            auto path3 = myfile.parent_path() / myfile;
            std::wcout << path1 << ", " << path3 << endl;
            //filesystem::rename(path1, path3);
        }
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多