简体   繁体   English

如何使用C ++在Linux中移动文件

[英]How to move file in Linux using C++

How do you move a file from one location to another using a C++ program in Linux? 如何在Linux中使用C ++程序将文件从一个位置移动到另一位置? I've written a program to do this and it runs, but when I try to move a file to a different directory it doesn't move the file, I get the error message from the cout statement. 我已经编写了一个程序来执行此操作,该程序可以运行,但是当我尝试将文件移动到其他目录时,它不会移动文件,但我从cout语句中得到了错误消息。 When I try to just rename the file, moving it to the same directory with a new name it works. 当我尝试重命名文件时,使用新名称将其移动到同一目录即可。 How can I fix my code so it will be able to move files to another directory? 如何修复我的代码,使其能够将文件移动到另一个目录?

Here's the code I've written: 这是我编写的代码:

#include <iostream>
#include <stdio.h>
using namespace std;

int main ()
{
  int result=1;
  char oldname[500];
  char newname[500];
  cout << "Enter the name of a file you want to move (include directory structure)";
  cin >> oldname;
  cout << "Enter the new location (include directory structure)";
  cin >> newname;

  result = rename( oldname , newname );
  if ( result == 0 )
    cout << "File successfully moved" << endl;
  else
    cout << "Error moving file" << endl;
  return 0;
}

Edit: I added perror to my code and the error message displayed is "Error moving file: No such file or directory" even though the directory I tried moving it to does exist and it has create and delete files permissions. 编辑:我在代码中添加了错误,并且显示的错误消息是“错误移动文件:没有这样的文件或目录”,即使我尝试将其移动到的目录确实存在并且具有创建和删除文件的权限。

Your code will work in most cases. 您的代码在大多数情况下都可以使用。 But you are ignoring some important things in which case it will break : 但是您忽略了一些重要的事情,在这种情况下,它会崩溃:

  • The obvious things like permissions, non-existing path, ... 显而易见的事情,例如权限,不存在的路径,...
  • Paths of 500 chars or more. 500个字符或更多的路径。 Don't use static allocated memory for oldname and newname 不要使用静态分配的内存oldnamenewname
  • Moving between filesystems is not possible with rename() so do it like this ( and include iostream ) 使用rename()无法在文件系统之间移动,因此必须这样做(并包含iostream

     ifstream ifs(oldname, ios::in | ios::binary); ofstream ofs(newname, ios::out | ios::binary); ofs << ifs.rdbuf(); remove(oldname); 

    Before the remove() your total disk space will be a bit less. remove()之前,您的总磁盘空间会少一点。

    • This doesn't matter if your are moving between filesystems because only the free space on the filesystem with newname will shrink and this is free space you have because otherwise you wouldn't able to move the file here 这在文件系统之间移动并不重要,因为只有具有newname的文件系统上的可用空间会缩小,而这是您拥有的可用空间,因为否则您将无法在此处移动文件
    • If oldname and newname are on the same filesystem and you really care about this temporary loss then check whether you'll be using the same filesystem and use rename() after all. 如果oldnamenewname在同一个文件系统上,并且您确实担心这种暂时的丢失,那么请检查您是否将使用同一个文件系统,并毕竟使用rename()

How to fix your program depends on the reason why the move (rename) failed. 如何修复程序取决于移动(重命名)失败的原因。

The reason for the failure can be found using errno . 使用errno可以找到失败的原因。


In this case, it was necessary to make sure that the source file exists. 在这种情况下,必须确保源文件存在。

For all things that need to be considered to robustly implement moving, I recommend studying an implementation of mv command. 对于稳固实现移动需要考虑的所有事项,我建议研究mv命令的实现。

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

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