C/C++文件剪切复制删除

47 篇文章 38 订阅

我们在写某些程序有破坏性的程序的时候,往往会对程序进行剪切复制删除等操作,

下面就来简单讲解下剪切复制删除,


文件的复制

#include <Windows.h>
#include <stdio.h>

int main()
{
	DWORD getlastError;
	if (!CopyFileA("C:\\1.txt", "F:\\1.txt", false))
	{
		printf_s("文件拷贝失败\n");
		getlastError = GetLastError();
		return -1;
	}
	return 0;
}





运行后我们就能发现能够把1.txt从C盘移动到F盘

下面来讲解下函数

CopyFile function

BOOL WINAPI CopyFile(
  _In_ LPCTSTR lpExistingFileName,
  _In_ LPCTSTR lpNewFileName,
  _In_ BOOL    bFailIfExists
);
第一个参数:一个存在文件的名字
第二个参数:新文件的名字
第三个参数:如果有同名的文件true则不进行复制,false为覆盖。
返回值:成功则返回非0数,失败返回0,并且调用GetLastError()可以获取错误信息.


下面是文件的删除代码
#include <Windows.h>
#include <stdio.h>

int main()
{
	DWORD getlastError;

	if (!DeleteFileA("C:\\1.txt"))
	{
		getlastError = GetLastError();
		printf_s("C:\\1.txt删除失败");
		return -1;

	}
	if (!DeleteFileA("F:\\1.txt"))
	{
		getlastError = GetLastError();
		printf_s("F:\\1.txt删除失败");
		return -1;
	}
	printf_s("删除成功\n");
        return 0;
}



DeleteFile function

BOOL WINAPI DeleteFile(
  _In_ LPCTSTR lpFileName
);

这里的参数是要被删除的文件的名字
返回值:
成功则返回非0数,失败返回0,并且调用GetLastError()可以获取错误信息.


下面是文件的剪切
#include <Windows.h>
#include <stdio.h>

int main()
{
	if (!MoveFileA("C:\\1.txt", "F:\\1.txt"))
	{
		DWORD getlasterror;
		getlasterror=GetLastError();
		printf_s("拷贝失败");
		return -1;
	}
	printf_s("拷贝成功\n");
	return 0;
}
函数的参数和返回值与上面那个相似,在此就不再说明了





  • 4
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
以下是一个简单的使用C++实现文件管理的示例程序。该程序可以列出指定目录下的所有文件和子目录,并支持文件复制剪切删除等基本操作。 ```c++ #include <iostream> #include <fstream> #include <string> #include <vector> #include <dirent.h> #include <unistd.h> using namespace std; const string ROOT_PATH = "/"; // 根目录路径 // 列出指定目录下的所有文件和子目录 vector<string> list_directory(const string& path) { vector<string> files; DIR* dir = opendir(path.c_str()); if (dir != nullptr) { struct dirent* entry; while ((entry = readdir(dir)) != nullptr) { string name = entry->d_name; if (name != "." && name != "..") { string full_path = path + "/" + name; files.push_back(full_path); } } closedir(dir); } return files; } // 复制文件或目录 bool copy_file(const string& src_path, const string& dst_path) { ifstream src(src_path, ios::binary); ofstream dst(dst_path, ios::binary); if (src && dst) { dst << src.rdbuf(); return true; } return false; } // 剪切文件或目录 bool move_file(const string& src_path, const string& dst_path) { if (rename(src_path.c_str(), dst_path.c_str()) == 0) { return true; } return false; } // 删除文件或目录 bool delete_file(const string& path) { if (remove(path.c_str()) == 0) { return true; } return false; } int main() { string current_path = ROOT_PATH; // 当前目录路径 vector<string> files = list_directory(current_path); while (true) { // 显示当前目录下的所有文件和子目录 cout << "Current directory: " << current_path << endl; for (int i = 0; i < files.size(); i++) { string name = files[i].substr(current_path.length() + 1); cout << (i + 1) << ". " << name << endl; } // 读取用户输入的命令 cout << "Enter command (ls/cp/mv/rm/cd/exit): "; string command; cin >> command; // 处理命令 if (command == "ls") { // 列出文件和子目录 files = list_directory(current_path); } else if (command == "cp") { // 复制文件或目录 string src_path, dst_path; cout << "Enter source path: "; cin >> src_path; cout << "Enter destination path: "; cin >> dst_path; if (copy_file(src_path, dst_path)) { cout << "Copy succeeded!" << endl; } else { cout << "Copy failed!" << endl; } } else if (command == "mv") { // 剪切文件或目录 string src_path, dst_path; cout << "Enter source path: "; cin >> src_path; cout << "Enter destination path: "; cin >> dst_path; if (move_file(src_path, dst_path)) { cout << "Move succeeded!" << endl; } else { cout << "Move failed!" << endl; } } else if (command == "rm") { // 删除文件或目录 string path; cout << "Enter path: "; cin >> path; if (delete_file(path)) { cout << "Delete succeeded!" << endl; } else { cout << "Delete failed!" << endl; } } else if (command == "cd") { // 切换目录 string path; cout << "Enter path: "; cin >> path; if (chdir(path.c_str()) == 0) { current_path = path; files = list_directory(current_path); } else { cout << "Change directory failed!" << endl; } } else if (command == "exit") { // 退出程序 break; } else { cout << "Invalid command!" << endl; } } return 0; } ``` 请注意,这只是一个简单的示例程序,实际应用中还需要考虑更多的细节和安全性问题。例如,需要检查文件是否存在、是否具有读写权限等。同时,需要对用户输入进行验证和过滤,以避免恶意操作和安全漏洞。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值