注册 登录
  • 注册时,本站名称为:RGB空格3S博客,注意中间的空格。
  • 2018.8.14本站全面接入google广告
  • 2017.2.14今天收到45条恶意评论(全是外文),故评论时请填写必要信息,匿名评论全部拉黑,迫不得已而为之
  • 2017.1.27,2017年春节及至,我谨代表本人祝大家新春快乐,本人年终总结文章请访问:2016年终总结
  • 为防止恶意转载,本站全面禁止复制,并添加图片水印:RGB 3S博客www.ixxin.cn。
  • 本站正式更名为RGB 3S博客,本站将撤消所有非3S内容,其将转移到新博客江湖时代

C++文件读写,字符串,vector容器练习

C/C++ admin 4415次浏览 已收录 0个评论
[隐藏]

前言

一个小练习,知识点还挺多的,主要功能就是读取一个文件夹下的特定格式文件,并把完整目录写入一个txt文件,程序虽小,知识点不少 :grin: .

代码

/*
作者:山科_xxin;
时间:2017年3月22日22:46:51;
功能:读取目录下文件,并把完整目录写入txt;
类别:<a href="https://www.ixxin.cn/tag/c%e5%ad%97%e7%ac%a6%e4%b8%b2/" title="查看更多关于C++字符串的文章" target="_blank">C++字符串</a>练习;
*/
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<io.h>
#include<vector>
#include<string>
using namespace std;


vector<string> GetFileName(const char *Path)
{
	vector<string>files;
	
	long handle = 0;
	struct _finddata_t fileinfo;
	handle = _findfirst(Path,&fileinfo);
	string stmp = Path;
	size_t a = stmp.find_last_of('\\');
	if (!string::npos) 
	{
		cout<<"查找失败";
	} 
	string t = "\\";
	string strName = stmp.substr(0,a)+t;

	if (handle==-1) 
	{
		cout<<"false";
		exit(0);
	}
	files.push_back(strName+fileinfo.name);
	while(!_findnext(handle,&fileinfo))
	{
		files.push_back(strName+fileinfo.name);
	}
	_findclose(handle);

	return files;
}

int SaveFname(vector<string>files,const char *Path)
{
	ofstream SFname(Path);
	int len = files.size();
	for (int i =0;i<len;i++)
	{
		cout<<files[i]<<endl;
		SFname<<files[i]<<endl;
	}
	SFname.close();
	return 0;
}

int main()
{
	char *Path;
	Path = "G:\\opencv\\*jpg";
	vector<string> files;
	files = GetFileName(Path);
	SaveFname(files,"G:\\opencv\\test.txt");
	system("pause");
	return 0;
}

main函数:
main
结果:
结果

后语

省略。


xxin blog , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++文件读写,字符串,vector容器练习
喜欢 (1)
支付宝[853060844@qq.com]
分享 (0)
admin
关于作者:
坐标山科大遥感系小鲜肉一枚。
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址