C/C++文件剪切复制删除

来源:互联网 发布:7004端口 编辑:程序博客网 时间:2024/06/08 05:51

我们在写某些程序有破坏性的程序的时候,往往会对程序进行剪切复制删除等操作,

下面就来简单讲解下剪切复制删除,


文件的复制

#include <Windows.h>#include <stdio.h>int main(){DWORD getlastError;if (!CopyFileA("C:\\1.txt", "F:\\1.txt", false)){printf_s("文件拷贝失败\n");getlastError = GetLastError();return -1;}return 0;}





运行后我们就能发现能够把1.txt从C盘移动到F盘

下面来讲解下函数

CopyFile function

BOOL WINAPI CopyFile(  _In_ LPCTSTR lpExistingFileName,  _In_ LPCTSTR lpNewFileName,  _In_ BOOL    bFailIfExists);
第一个参数:一个存在文件的名字
第二个参数:新文件的名字
第三个参数:如果有同名的文件true则不进行复制,false为覆盖。
返回值:成功则返回非0数,失败返回0,并且调用GetLastError()可以获取错误信息.


下面是文件的删除代码
#include <Windows.h>#include <stdio.h>int main(){DWORD getlastError;if (!DeleteFileA("C:\\1.txt")){getlastError = GetLastError();printf_s("C:\\1.txt删除失败");return -1;}if (!DeleteFileA("F:\\1.txt")){getlastError = GetLastError();printf_s("F:\\1.txt删除失败");return -1;}printf_s("删除成功\n");        return 0;}



DeleteFile function

BOOL WINAPI DeleteFile(  _In_ LPCTSTR lpFileName);

这里的参数是要被删除的文件的名字
返回值:
成功则返回非0数,失败返回0,并且调用GetLastError()可以获取错误信息.


下面是文件的剪切
#include <Windows.h>#include <stdio.h>int main(){if (!MoveFileA("C:\\1.txt", "F:\\1.txt")){DWORD getlasterror;getlasterror=GetLastError();printf_s("拷贝失败");return -1;}printf_s("拷贝成功\n");return 0;}
函数的参数和返回值与上面那个相似,在此就不再说明了





1 0