一、文件的复制
#include <iostream>
#include <fstream>
using namespace std;
int CopyFile(char *SourceFile,char *NewFile)
{
ifstream in;
ofstream out;
in.open(SourceFile,ios::binary);//打开源文件
if(in.fail())//打开源文件失败
{
cout<<"Error 1: Fail to open the source file."<<endl;
in.close();
out.close();
return 0;
}
out.open(NewFile,ios::binary);//创建目标文件
if(out.fail())//创建文件失败
{
cout<<"Error 2: Fail to create the new file."<<endl;
out.close();
in.close();
return 0;
}
else//复制文件
{
out<<in.rdbuf();
out.close();
in.close();
return 1;
}
}
void main()
{
char source[256],NewFile[256];
cout<<"请输入要复制的文件路径:"<<endl;
cin>>source;
cout<<"请输入新文件的路径:"<<endl;
cin>>NewFile;
if(CopyFile(source,NewFile))
{
cout<<"文件已成功复制..."<<endl;
}
else
{
cout<<"文件复制失败..."<<endl;
}
cin.get();
cin.get();
}
二、文件的删除
#include <iostream.h>
#include <windows.h>
#include <io.h>
void main()
{
char source[256];//文件路径
cout<<"请输入要删除的文件路径:"<<endl;
cin>>source;
/* _access(char *,int) 判断文件是否存在
存在 返回0;不存在 返回-1.
_access(const char *path,int mode)
mode的值:
00 是否存在
02 写权限
04 读权限
06 读写权限
*/
if(!_access(source,0))//如果文件存在:文件为只读无法删除
{
//去掉文件只读属性
SetFileAttributes(source,0);
if(DeleteFile(source))//删除成功
{
cout<<source<<" 已成功删除."<<endl;
}
else//无法删除:文件只读或无权限执行删除
{
cout<<source<<" 无法删除:文件为只读属性或无删除权限."<<endl;
}
}
else//文件不存在
{
cout<<source<<" 不存在,无法删除."<<endl;
}
cin.get();
}
三 文件的重命名
#include <iostream.h>
#include <windows.h>
#include <io.h>
void main()
{
char source[256];//文件路径
char newname[256];
cout<<"请输入要重命名的文件路径:"<<endl;
cin>>source;
cout<<"请输入文件的新名称:"<<endl;
cin>>newname;
if(!_access(source,0))//如果文件存在:
{
if(!rename(source,newname))//删除成功
{
cout<<source<<" 成功重命名为: "<<newname<<endl;
}
else//无法重命名:文件打开或无权限执行重命名
{
cout<<"文件无法重命名(可能原因如下):"<<endl;
cout<<"\t"<<"1. "<<newname<<" 已存在"<<endl
<<"\t"<<"2. "<<newname<<" 正在使用,未关闭."<<endl
<<"\t"<<"3. "<<"你没有权限重命名此文件."<<endl;
}
}
else//文件不存在
{
cout<<source<<" 不存在,无法重命名."<<endl;
}
cin.get();
}
//=====================================================================================
利用 c 语言的库的办法:
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN:
int _access( const char *path, int mode );
Return Value
Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:
EACCES
Access denied: file’s permission setting does not allow specified access.
ENOENT
Filename or path not found.
Parameters
path
File or directory path
mode
Permission setting
Remarks
When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.
mode Value Checks File For
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
Example
/* ACCESS.C: This example uses _access to check the * file named "ACCESS.C" to see if it exists and if * writing is allowed. */ #include < io.h > #include < stdio.h > #include < stdlib.h > void main( void ) { /* Check for existence */ if ( (_access( " ACCESS.C " , 0 )) != - 1 ) { printf( " File ACCESS.C exists " ); /* Check for write permission */ if ( (_access( " ACCESS.C " , 2 )) != - 1 ) printf( " File ACCESS.C has write permission " ); } }
Output
File ACCESS.C existsFile ACCESS.C has write permission
3.在windows平台下用API函数FindFirstFile(...):
(1)检查文件是否存在:
#include " windows.h "
int
main( int argc, char * argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ( " Target file is %s. " , argv[ 1 ]);
hFind = FindFirstFile(argv[ 1 ], & FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf ( " Invalid File Handle. Get Last Error reports %d " , GetLastError ());
} else {
printf ( " The first file found is %s " , FindFileData.cFileName);
FindClose(hFind);
}
return ( 0 );
}
(2)检查某一目录是否存在:
bool CheckFolderExist( const string & strPath)
{
WIN32_FIND_DATA wfd;
bool rValue = false ;
HANDLE hFind = FindFirstFile(strPath.c_str(), & wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
rValue = true ;
}
FindClose(hFind);
return rValue;
}
4.使用boost的filesystem类库的exists函数
#include < boost / filesystem / path.hpp >
#include < boost / filesystem / convenience.hpp >
int GetFilePath(std:: string & strFilePath)
{
string strPath;
int nRes = 0 ;
// 指定路径
strPath = " D:\myTest\Test1\Test2 " ;
namespace fs = boost::filesystem;
// 路径的可移植
fs::path full_path( fs::initial_path() );
full_path = fs::system_complete( fs::path(strPath, fs::native ) );
// 判断各级子目录是否存在,不存在则需要创建
if ( ! fs::exists( full_path ) )
{
// 创建多层子目录
bool bRet = fs::create_directories(full_path);
if ( false == bRet)
{
return - 1 ;
}
}
strFilePath = full_path.native_directory_string();
return 0 ;
}