C++函数

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]; //文件名  
};
*/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值