CopyFile 无法找到该文件,而我可以从 fopen 打开它

CopyFile cant found the file while i can open it from fopen

我尝试创建一个程序来创建文件夹的备份。问题是,当我尝试使用 CopyFile 函数时出现错误 2 (FILE_NOT_FOUND),但我可以使用 fopen 和完全相同的路径打开文件。我也是用utf-8格式的。


void Folder::copy_files(std::string destination) {

    bool error = false;
    std::string destinationpath = destination;
    for (std::string i : Get_files_paths()) {
        std::string destinationpath = destination;
        destinationpath.append(split_file_folder_name(i));


#ifdef DEBUG
        char str[100];
        const char* floc_cstr = i.c_str();
        LPCTSTR floc = (LPCTSTR)floc_cstr;
        printf("\t[DEBUG]FILE_LOC_PATH: %s\n", floc_cstr);
        std::cout << "\t[DEBUG]memory loc" << floc << std::endl;
#pragma warning(disable : 4996)
        FILE* fp = fopen(floc_cstr, "r");
        if (fp == NULL) {
            printf("file not found");
            exit(1);
        }
        else {
            printf("file found \n");
            fscanf(fp, "%s", str);
            printf("%s", str);
        }
        fclose(fp);
        print_last_error(GetLastError());
#endif
        error = CopyFile(floc , (LPCTSTR)destinationpath.c_str(), false);
        if (error == false) {
            print_last_error(GetLastError());
        }
    }

}

根据这段代码,我应该希望复制文件,但我得到了 FILE_NOT_FOUND。 有人知道为什么会这样吗? (如果您需要代码的任何其他部分,请告诉我)

感谢评论中的帮助,解决方案是使用 std::filesystem::copy_file(i,destinationpath); 而不是
CopyFile(floc , (LPCTSTR)destinationpath.c_str(), false); wstring 的使用不是必需的。所以现在的代码是这样的:

void Folder::copy_files(std::string destination) {
    const char* floc_cstr = NULL;
    bool error = false;
    std::string destinationpath;
    for (std::string i : Get_files_paths()) {
        try {
            destinationpath = destination;
            destinationpath.append(split_file_folder_name(i));
            error = fs::copy_file(i, destinationpath);
            if (error == false) {
                print_last_error(GetLastError());
            }
        }
        catch(std::exception e) {
#ifdef DEBUG
            std::cout << "[DEBUG]" << e.what() <<std::endl;
#endif
            std::cout << "file exist\n";
            continue;
        }
    }
}