文件操作篇(2.文件的复制 移动 删除)

来源:互联网 发布:js字符串去重 编辑:程序博客网 时间:2024/06/04 19:20

前一段时间因为笔记本坏了,所以也一直没更新文章,请见谅。重新买了个电脑,因为用的1080P的屏幕,所以VC6.0用着就再也不愉快了,以后程序我主要使用VS写,无论什么环境下面,其实现还是WINAPI的那些东西,读者对此不要太在意。
也是从这一个博客开始,我将尽可能多的给大家展MSDN上对该函数的官方解释。一方面帮助大家多学习一点英语,一方面接受第一方信息,不会像很多书籍里面对函数的解释模糊不清甚至错误。但是注意,我的英语水平有限,读者见谅,大神绕道。

1:文件的拷贝

BOOL CopyFile(    LPCTSTR lpExistingFileName,    LPCTSTR lpNewFileName,    BOOL bFaillfExists);

Parmeters
lpExistingFileName :The name of an existing file,
if lpExistingFileName does not exist, CopyFile fails, and GetLastError returns ERROR_FILE_NOT_FOUND.
一个存在文件的文件名。
如果这个名字指定的文件不存在,CopyFile()调用失败,并且GetLastError()函数将返回 ERROR_FILE_NOT_FOUND

lpNewFileName :The name of the new file.
参数指向复制到目标位置的新文件名 (完整路径)。

bFailIfExists
If this parameter is TRUE and the new file specified by lpNewFileName already exists, the function fails. If this parameter isFALSE and the new file already exists, the function overwrites the existing file and succeeds.
如果这个参数是TRUE并且lpNewFileName 指定的新文件已经存在时,该函数调用失败。如果这个参数是FALSE并且这个新文件已经存在,该函数将重写这个已经存在的文件,成功调用。

Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
如果函数调用成功,返回值非零(TRUE)
如果函数调用失败,返回值为零(FALSE), 调用GetLastError()函数可以获取更多错误信息。

#include <windows.h>#include <stdio.h>#include <tchar.h>//#include <tchar.h>是为了使用_T()宏int main(){    bool status = CopyFile(_T("D://haozip.exe"), _T("E://haozip.exe"), TRUE);    if (status)    {        printf("Copy file succeed!");    }    else    {        printf("Copy file failed!");    }    return 0;}

2:文件的移动

BOOL MoveFile(    LPCTSTR lpExistingFileName,    LPCTSTR lpNewFileName);

Parameters
lpExistingFileName:The current name of the file or directory on the local computer。本地计算机上的文件或者是一个目录

lpNewFileName:The new name for the file or directory. The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive.
给文件或者是目录指定的新名称。这个新名称一定不能存在。可以将一个文件移动到一个不同的文件系统或者是磁盘,但是对于目录来说只能移动到相同的磁盘下面。

Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
如果函数调用成功,返回值非零(TRUE)
如果函数调用失败,返回值为零(FALSE), 调用GetLastError()函数可以获取更多错误信息

#include <windows.h>#include <stdio.h>#include <tchar.h>//#include <tchar.h>是为了使用_T()宏int main(){    /*进行文件夹移动        在test文件夹中我创建了几个文件        文件夹移动的时候文件夹中的内容也将被移动到新的目录下面    */    BOOL status = MoveFile(_T("D://test"), _T("D://hi//test1"));    if (status)    {        printf("Move to another folder succeed!");    }    else    {        printf("Move to another folder failed!");    }    /*文件移动        文件移动的时候一定要指明其完整路径    */    status = MoveFile(_T("D://test.txt"), _T("E://test.txt"));    if (status)    {        printf("Move to another folder succeed!");    }    else    {        printf("Move to another folder failed!");    }    system("pause");    return 0;}

3:删除文件

BOOL DeleteFile(    LPCTSTR lpFileName);

Parameters
lpFileName 这个参数为文件的完整路径

每日箴言:世上无难事,只要不要脸。

0 0
原创粉丝点击