Vc - Tool - 排序乱序的行

步骤一:在桌面中添加两个txt文件,分别命名为read.txt及write.txt

在read.txt添加下面内容:

2他好
3她好
1我好
4大家好
5世界人民都好

步骤二:新建空的控制台项目,添加一个ReadFileAndWriteFile.hpp文件及main.cpp文件

ReadFileAndWriteFile.hpp文件内容如下:

#ifndef  ReadFileAndWriteFile_Hpp
#define  ReadFileAndWriteFile_Hpp
#include <string>
#include <iostream>
#include <fstream>
#include <map>
#include <regex>
#include <vector>
class ReadFileAndWriteFile
{
	//成员变量
private:
	std::string  m_strReadFile;
	std::string  m_strWriteFile;
	std::vector<std::string>  m_strFileList;
	std::map<int, std::string> m_LineMap;
	//成员函数
	//构造函数
public:
	ReadFileAndWriteFile(std::string strReadFileName,std::string strWriteFileName)
	{
		m_strReadFile = strReadFileName;
		m_strWriteFile = strWriteFileName;
	}
	//成员方法
public:
	std::string getStrReadFile()
	{
		return m_strReadFile;
	}
	void setStrReadFile(std::string strFileName)
	{
		m_strReadFile = strFileName;
	}
	void setWriteFile(std::string strFileName)
	{
		m_strWriteFile = strFileName;
	}
	void printLineMapValue()
	{
		//

	}
	void Init( )
	{
		std::ifstream inFile(m_strReadFile.c_str());
		if (!inFile)
		{
			std::cout << "error unable to open infile!" << std::endl;
		}
		std::ofstream outFile(m_strWriteFile.c_str());
		if (!outFile)
		{
			std::cout << "error unable to open outfile!" << std::endl;
		}
		///按行读取文件中的值
		std::string strTempLine;
		while (getline(inFile, strTempLine))
		{
			m_strFileList.push_back(strTempLine);
			std::regex pattern1("\\d{1,2}");
			std::string strTemp1;
			std::string strTemp2;
			strTemp1 = std::regex_replace(strTempLine, pattern1, "");
			std::regex pattern2(strTemp1);
			strTemp2 = std::regex_replace(strTempLine, pattern2, "");
			std::cout << "string value:" << strTempLine <<std::endl;
			//std::cout << strTemp1 << std::endl;
			//std::cout << strTemp2 << std::endl;
			int nStrTemp2 = atoi(strTemp2.c_str());
			m_LineMap.insert(std::make_pair(nStrTemp2, strTempLine));
		}
		///创建迭代器,循环遍历
		for (std::map<int, std::string>::iterator it = m_LineMap.begin(); it != m_LineMap.end(); it++)
		{
			///向文件中进行写入数据
			outFile <<it->second << "\n";
		}
		///
		inFile.close();
		outFile.close();
	}
};
#endif // ! ReadFileAndWriteFile_Hpp

main.cpp内容如下:

#include"ReadFileAndWriteFile.hpp"

int main(int argc,char **argv)
{

	ReadFileAndWriteFile fw("C://Users//Administrator//Desktop//read.txt",
		"C://Users//Administrator//Desktop//write.txt");
	
	fw.Init();

	return 0;
}

步骤三:运行得到write.txt中的内容如下:

1我好
2他好
3她好
4大家好
5世界人民都好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

莫忘输赢

莫忘输赢 - 收钱袋

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

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

打赏作者

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

抵扣说明:

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

余额充值