一、文件的复制
#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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2012-09-16 RTP/RTSP/RTCP的区别