博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC++中windows下的文件复制、删除、重命名操作
阅读量:3987 次
发布时间:2019-05-24

本文共 1944 字,大约阅读时间需要 6 分钟。

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

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

一、文件的复制

[cpp]   
  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]   
  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]   
  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. }  
你可能感兴趣的文章
No.182 - LeetCode1325 - C指针的魅力
查看>>
No.183 - LeetCode1324
查看>>
mac:移动python包路径
查看>>
mysql:sql create database新建utf8mb4 数据库
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql alter table 修改列属性的字符集
查看>>
mysql:sql drop database 删除数据库
查看>>
mysql:sql character set utf8mb4 新建utf8mb4表
查看>>
mysql:sql drop table (删除表)
查看>>
mysql:sql truncate (清除表数据)
查看>>
mysql:sql order by */* desc (查询)
查看>>
scrapy:xpath string(.)非常注意问题
查看>>
剑指Offer:字符流中第一个不重复的字符
查看>>
剑指Offer:链表中环的入口节点
查看>>
剑指Offer:数据流中的中位数
查看>>
剑指Offer:滑动窗口的最大值
查看>>
Qt组态图片区域事件响应
查看>>
一张图片与它的掩码 mask 蒙板
查看>>
GIMP 画布适应选区
查看>>
QSqlQuery执行多条Sql语句的方法
查看>>