我们在写某些程序有破坏性的程序的时候,往往会对程序进行剪切复制删除等操作,
下面就来简单讲解下剪切复制删除,
文件的复制
#include <Windows.h>
#include <stdio.h>
int main()
{
DWORD getlastError;
if (!CopyFileA("C:\1.txt", "F:\1.txt", false))
{
printf_s("文件拷贝失败
");
getlastError = GetLastError();
return -1;
}
return 0;
}
运行后我们就能发现能够把1.txt从C盘移动到F盘
下面来讲解下函数
立即学习“C++免费学习笔记(深入)”;
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("删除成功
");
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("拷贝成功
");
return 0;
}函数的参数和返回值与上面那个相似,在此就不再说明了
以上就是 C/C++文件剪切复制删除的内容,更多相关内容请关注PHP中文网(www.php.cn)!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号