1、复制文件
#include "windows.h"
void CPfile(string in, string out){
WCHAR buf[256];
memset(buf ,0, sizeof(buf));
MultiByteToWideChar(CP_ACP, 0, in.c_str(),strlen(in.c_str()+1),buf,sizeof(buf)/sizeof(buf[0]));
WCHAR buf2[256];
memset(buf2, 0, sizeof(buf2));
MultiByteToWideChar(CP_ACP, 0, out.c_str(), strlen(out.c_str() + 1), buf2, sizeof(buf2) / sizeof(buf2[0]));
int ret = CopyFile(buf, buf2, false);
}
2、将字符串按空格分割
void splite(string in,vector<string>res){
string result;
stringstream input(in);
while (input >> result){
res.push_back(result);
}
}
3、获得目录下的文件路径和子目录
#include <iostream>
#include <vector>
#include <string>
#include <io.h>
using namespace std;
inline void getfileall(string path, vector<string> &filepath, vector<string> &folder){
struct _finddata_t fileinfo; //_finddata_t是一个结构体,要用到#include <io.h>头文件;
long ld;
if ((ld = _findfirst((path + "\\*").c_str(), &fileinfo)) != -1l){
do{
if ((fileinfo.attrib&_A_SUBDIR)){ //如果是文件夹;
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0){ //如果文件名不是.或者,,,则递归获取子文件中的文件;
folder.push_back(path + "\\" + fileinfo.name);
getfileall(path + "\\" + fileinfo.name, filepath, folder); //递归子文件夹;
}
}
else //如果是文件;
{
filepath.push_back(path + "\\" + fileinfo.name);
//cout << path+"\\"+fileinfo.name << endl;
}
} while (_findnext(ld, &fileinfo) == 0);
_findclose(ld);
}
}
int main(){
string path = "";
vector<string> filepath;
vector<string> folder;
getfileall(path, filepath, folder);
for (int i = 0; i < folder.size(); i++){
cout << folder[i] << endl;
}
return 0;
}
/*在判断有无子目录的if分支中,由于系统在进入一个子目录时,匹配到的头两个文件(夹)是"."(当前目录),".."(上一层目录)。需要忽略掉这两种情况。当需要对遍历到的文件做处理时,在else分支中添加相应的代码就好*/
/*
struct _finddata_t
{
unsigned attrib; //文件属性
time_t time_create; //文件创建时间
time_t time_access; //文件上一次访问时间
time_t time_write; //文件上一次修改时间
_fsize_t size; //文件字节数
char name[_MAX_FNAME]; //文件名
};
*/