VC++中windows下的文件复制、删除、重命名操作

来源:互联网 发布:网络运营主管岗位职责 编辑:程序博客网 时间:2024/06/05 07:23

转自:http://blog.csdn.net/mmjwung/article/details/8499802

都可以很方便的通过windows.h中的函数来实现

一、文件的复制

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4. int CopyFile(char *SourceFile,char *NewFile)  
  5. {  
  6.   ifstream in;  
  7.   ofstream out;  
  8.   in.open(SourceFile,ios::binary);//打开源文件  
  9.   if(in.fail())//打开源文件失败  
  10.   {  
  11.      cout<<"Error 1: Fail to open the source file."<<endl;  
  12.      in.close();  
  13.      out.close();  
  14.      return 0;  
  15.   }  
  16.   out.open(NewFile,ios::binary);//创建目标文件   
  17.   if(out.fail())//创建文件失败  
  18.   {  
  19.      cout<<"Error 2: Fail to create the new file."<<endl;  
  20.      out.close();  
  21.      in.close();  
  22.      return 0;  
  23.   }  
  24.   else//复制文件  
  25.   {  
  26.      out<<in.rdbuf();  
  27.      out.close();  
  28.      in.close();  
  29.      return 1;  
  30.   }  
  31. }  
  32. void main()  
  33. {  
  34.   char source[256],NewFile[256];  
  35.   cout<<"请输入要复制的文件路径:"<<endl;  
  36.   cin>>source;  
  37.   cout<<"请输入新文件的路径:"<<endl;  
  38.   cin>>NewFile;  
  39.   if(CopyFile(source,NewFile))  
  40.   {  
  41.      cout<<"文件已成功复制..."<<endl;  
  42.   }  
  43.   else  
  44.   {  
  45.      cout<<"文件复制失败..."<<endl;  
  46.   }  
  47. }  
二、文件的删除

[cpp] view plain copy
  1. #include <iostream.h>  
  2. #include <windows.h>  
  3. #include <io.h>  
  4. void main()  
  5. {  
  6.   char source[256];//文件路径  
  7.   cout<<"请输入要删除的文件路径:"<<endl;  
  8.   cin>>source;  
  9. /* _access(char *,int) 判断文件是否存在 
  10. 存在 返回0;不存在 返回-1. 
  11. _access(const char *path,int mode) 
  12. mode的值: 
  13. 00 是否存在 
  14. 02 写权限 
  15. 04 读权限 
  16. 06 读写权限 
  17. */  
  18.   if(!_access(source,0))//如果文件存在:文件为只读无法删除  
  19.   {  
  20.   //去掉文件只读属性  
  21.   SetFileAttributes(source,0);  
  22.   if(DeleteFile(source))//删除成功  
  23.   {  
  24.      cout<<source<<" 已成功删除."<<endl;  
  25.   }  
  26.   else//无法删除:文件只读或无权限执行删除  
  27.   {  
  28.      cout<<source<<" 无法删除:文件为只读属性或无删除权限."<<endl;  
  29.   }  
  30.   }  
  31.   else//文件不存在  
  32.   {  
  33.     cout<<source<<" 不存在,无法删除."<<endl;  
  34.   }  
  35. }  
三 文件的重命名
[cpp] view plain copy
  1. #include <iostream.h>  
  2. #include <windows.h>  
  3. #include <io.h>  
  4. void main()  
  5. {  
  6.   char source[256];//文件路径  
  7.   char newname[256];  
  8.   cout<<"请输入要重命名的文件路径:"<<endl;  
  9.   cin>>source;  
  10.   cout<<"请输入文件的新名称:"<<endl;  
  11.   cin>>newname;  
  12.   if(!_access(source,0))//如果文件存在:  
  13.   {  
  14.     if(!rename(source,newname))//删除成功  
  15.     {  
  16.        cout<<source<<" 成功重命名为: "<<newname<<endl;  
  17.     }  
  18.     else//无法重命名:文件打开或无权限执行重命名  
  19.     {  
  20.        cout<<"文件无法重命名(可能原因如下):"<<endl;  
  21.        cout<<"\t"<<"1. "<<newname<<" 已存在"<<endl  
  22.          <<"\t"<<"2. "<<newname<<" 正在使用,未关闭."<<endl  
  23.          <<"\t"<<"3. "<<"你没有权限重命名此文件."<<endl;  
  24.     }  
  25.   }  
  26.   else//文件不存在  
  27.   {  
  28.     cout<<source<<" 不存在,无法重命名."<<endl;  
  29.   }  
  30. }  
0 0
原创粉丝点击