C/C++文件剪切复制删除
生活随笔
收集整理的這篇文章主要介紹了
C/C++文件剪切复制删除
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們?cè)趯懩承┏绦蛴衅茐男缘某绦虻臅r(shí)候,往往會(huì)對(duì)程序進(jìn)行剪切復(fù)制刪除等操作,
下面就來(lái)簡(jiǎn)單講解下剪切復(fù)制刪除,
文件的復(fù)制
#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; }
運(yùn)行后我們就能發(fā)現(xiàn)能夠把1.txt從C盤移動(dòng)到F盤
下面來(lái)講解下函數(shù)
CopyFile function
BOOL WINAPI CopyFile(_In_ LPCTSTR lpExistingFileName,_In_ LPCTSTR lpNewFileName,_In_ BOOL bFailIfExists );第一個(gè)參數(shù):一個(gè)存在文件的名字 第二個(gè)參數(shù):新文件的名字 第三個(gè)參數(shù):如果有同名的文件true則不進(jìn)行復(fù)制,false為覆蓋。 返回值:成功則返回非0數(shù),失敗返回0,并且調(diào)用GetLastError()可以獲取錯(cuò)誤信息.下面是文件的刪除代碼 #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 );這里的參數(shù)是要被刪除的文件的名字 返回值: 成功則返回非0數(shù),失敗返回0,并且調(diào)用GetLastError()可以獲取錯(cuò)誤信息.
下面是文件的剪切 #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; }函數(shù)的參數(shù)和返回值與上面那個(gè)相似,在此就不再說(shuō)明了
總結(jié)
以上是生活随笔為你收集整理的C/C++文件剪切复制删除的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql查询1971年以前_mysql
- 下一篇: php登录注册脚本,PHP - 登录脚