如何在 C++ 中将文件从 ext4 文件系统重命名为 ntfs 文件系统
How to rename a file from ext4 file system to ntfs file system in C++
有一个查询。我总是使用 C++ 中的代码(如下(将文件从同一驱动器中的一个位置移动到另一个位置(称为 A 驱动器(
rename(const char* old_filename, const char* new_filename);
最近我需要修改代码以将其移动到另一个驱动器(调用 B-Drive(。它不起作用,但我可以编写代码以写入该特定驱动器(B 驱动器(。经过调查,我发现我产生结果(旧文件(的驱动器(A 驱动器(在 ext4 文件系统中,但我正在写入/移动到的驱动器在 NTFS (fuseblk( 中
如何修改我的代码以将文件移动到 NTFS。我在 ubuntu 中使用C++
问候
--------------------------------------------------------------------
新 听从用户4581301的呼叫后编辑
这是我写的代码
int main()
{
std::string dirinADrive = "/home/akaa/data/test3/test_from.txt"; // this is the parent directory
std::string dirinBDrive = "/media/akaa/Data/GIRO_repo/working/data/test5/test_to.txt"; // this is where i want to write to
std::string dirinCDrive = "/home/akaa/data/test3/test_to.txt"; // this is where i want to write to
std::string dirinDDrive = "/media/akaa/Data/GIRO_repo/working/data/test5/test_to_write.txt";
bool ok1{std::ofstream(dirinADrive).put('a')}; // create and write to file
bool ok2{std::ofstream(dirinDDrive).put('b')}; // create and write to file
if (!(ok1 && ok2))
{
std::perror("Error creating from.txt");
return 1;
}
if (std::rename(dirinADrive.c_str(), dirinCDrive.c_str())) // moving file to same drive
{
std::perror("Error renaming local");
return 1;
}
if (std::rename(dirinADrive.c_str(), dirinBDrive.c_str())) // moving file to other drive
{
std::perror("Error renaming other");
return 1;
}
std::cout << std::ifstream(dirinBDrive).rdbuf() << 'n'; // print file
}
我得到了一个错误
Error renaming other: Invalid cross-device link
那么什么是无效的跨设备链接?
谢谢
您不能跨文件系统使用rename
,因为必须复制数据(即使没有原子性问题,让单个系统调用执行任意数量的工作也是有问题的(。 您确实必须打开源文件和目标文件,并将一个文件的内容写入另一个。 应用要保留的任何属性(例如,使用 stat
和 chmod
(,然后根据需要删除源文件。
在 C++17 中,其中大部分已被打包为 std::filesystem::copy_file
. (也有std::filesystem::rename
,但在这种情况下并不比std::rename
更好。
相关文章:
- 文件系统:复制功能的速度秘诀是什么
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 如何传递多个 std::文件系统选项?
- 遍历顺序由 std::文件系统directory_iterator给出
- libstdc++ 文件系统中未初始化的用法?
- boost::文件系统::recursive_directory_iterator多线程安全
- C++17 文件系统::remove_all 带有通配符路径
- 实验性文件系统库不完整C++?
- 使用文件系统时仍然需要链接到带有 C++20 的 stdc++fs?
- 无法链接文件系统库C++
- 无法识别 Mac c++ 文件系统库
- 使用Boost文件系统C++将具有特定扩展名的文件的名称保存在特定文件夹中
- 获取 clang++:错误:在编译文件系统库的代码时
- C++17 文件系统在 Windows 10 上使用 nuwen MinGW
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 如何修复 MinGW 文件系统探针?
- 将 boost::文件系统::p ath 转换为 char?
- 我可以在 ndk 中使用带有 libc++ 的实验性文件系统吗?
- 是否可以使用 std::文件系统获取根名称列表?
- 如何在 C++ 中将文件从 ext4 文件系统重命名为 ntfs 文件系统