c 编程:程序将"桌面"文件夹读取为大写字母 E 的"DEsktop"



所以我正在构建的C程序必须放在我的MAC计算机(OS X 10.9.4)桌面上的PA_mobile文件中,以便它可以访问与之关联的脚本和文本文件缓存。

现在,当我的程序启动时,它会验证当前工作目录,将其与包含正确工作目录的字符串变量进行比较,如果程序不在桌面中,则请求更改工作目录。

以下是与目录路径验证关联的代码:

char* cwd_2;
char buff[PATH_MAX + 1];
cwd_2 = getcwd( buff, PATH_MAX + 1 );
char desired_working_directory[50] = "/Users/haydn/Desktop/PA_mobile";
if( cwd_2 != NULL ) 
{
    printf("n-----------------------------------");
    printf("nCurrent working directory: [%s]n", cwd_2 );
    usleep(10000);
    printf("Desired working directory: [%s]n",desired_working_directory);
    usleep(50000);
    if(strcmp(desired_working_directory, cwd_2) == 0) 
    {
        fflush(stdout);
        printf("Appropriate Working Directory achieved!n");
        printf("-----------------------------------");
    }
    else
    {
        printf("Critical Error: inapropriate working directory!n");
        printf("please relocate the 'PA_mobile' file to [/users/user/Desktop] and re-launchn");
        getchar();
        exit();
    }
}

程序比较由 cwd_2 变量确定的当前工作目录路径,并将其与所需的工作目录路径进行比较,如 desired_working_directory 变量中所述。

现在这是程序运行时显示的内容:

-----------------------------------
Current working directory: [/users/haydn/DEsktop/PA_mobile]
Desired working directory: [/Users/haydn/Desktop/PA_mobile]
Critical Error: inapropriate working directory!
please relocate file to [/users/haydn/Desktop] and re-launch

请注意,显示的当前工作目录(由 getcwd( buff, PATH_MAX + 1 ); 函数确定)将桌面文件描述为DEsktop,而不是Desktop。 我已经检查了实际的桌面文件夹,它的拼写没有大写的 E,大写的 E 似乎是导致问题的, 并且只存在于这段代码中,有人可以帮助我吗?

如果您运行diskutil list /(或在Mac OSX的更高变体上运行diskutil info /),您将看到文件系统是否区分大小写。您可能需要在层次结构中低于/的文件系统上执行此操作,如果这些目录位于单独的文件系统中。

您还可以尝试以下命令序列:

touch /Users/haydn/Desktop/xyzzy
touch /Users/haydn/Desktop/xyzzY
ls /Users/haydn/Desktop/xyzz*

并查看有多少文件。一个表示不区分大小写,两个表示区分大小写。

这是

文件系统个性决定了这一点,除非它提到区分大小写,否则您可以假设它不是。

在这种情况下,这些之间没有区别:

/users/haydn/DEsktop/PA_mobile
/Users/haydn/Desktop/PA_mobile
 ^            ^

尽管字母不同。

如果您已经确定这是问题所在,那么您的代码实际上应该进行调整以处理这两种情况。这个问题(及其答案)提供了从代码中执行此操作的方法。

最新更新