简体   繁体   English

Windows(c ++)上文件路径中的特殊字符

[英]Special Characters in File Paths on Windows (c++)

I seem to be stuck on special characters (like äöü) in windows file paths. 我似乎被困在Windows文件路径中的特殊字符(如äöü)上。 They are legal names for folders (users can set them). 它们是文件夹的合法名称(用户可以设置它们)。

A part of my program has to be able to traverse the filesystem. 我程序的一部分必须能够遍历文件系统。 When trying to enter a childfolder with the name 'öö' (testcase) I get the error that the directory does not exist. 尝试输入名称为“öö”(测试用例)的子文件夹时,出现错误提示:该目录不存在。

I am rather sure that the problem is this 'line': 我相当确定问题出在这条“线”上:

wstring newPath = oldPath + ffd.cFileName + L"\\";

From

void reevaluateJob(wstring newPath) {
    WIN32_FIND_DATA ffd;
    HANDLE findFileHandle = FindFirstFile((newPath + L"\*").c_str(), &ffd);
    //skipping invalid case handling and loop
    if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
        if ((wcscmp(ffd.cFileName, L".") != 0) && (wcscmp(ffd.cFileName, L"..") != 0))
            reevaluateJob(newPath + ffd.cFileName + L"\\");  //<=== new path here
    } else {
        //skipping file part
    }
}

Because printing the new path (or ffd.cFileName as wstring) results in different characters. 因为打印新路径(或ffd.cFileName作为wstring)会导致字符不同。 Is there another data type that doesn't have this problem? 是否存在另一种没有此问题的数据类型?

EDIT: This works totally fine as long as the folder names do not contain special characters (like äöü etc.). 编辑:只要文件夹名称不包含特殊字符(如äöü等),此方法就可以正常工作。

As pointed out by @ArnonZilca, #define UNICODE solved half of the problem. 正如@ArnonZilca指出的那样,#define #define UNICODE解决了一半的问题。 Sadly not all windows functions stick to that rule. 遗憾的是,并非所有Windows函数都遵守该规则。 Some also want #define _UNICODE . 有些人也想要#define _UNICODE

In the process of trying to fix the problem, I also changed my whole code to use WCHAR* instead of wstring. 在尝试解决问题的过程中,我还将整个代码更改为使用WCHAR *而不是wstring。 I assume (but cannot be 100% sure) that this does NOT make a difference. 我假设(但不能100%确定)这不会有所作为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM