C++将文本文件转化为单串并查询

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Dict
{
private:
   	ifstream f1;
   	ofstream f2;
   	string s,temp,word;
public:
	Dict()
	{
		f1.open("英汉词典.txt",ios::in);
		f2.open("单串英汉词典.txt",ios::out);
		s="-";
		while(!f1.eof())//用“-”将单词隔开
		{
			getline(f1,temp);
			s=s+temp+"-";
		}
		f2<<s;
	}
	void find_w()
	{
		cout<<"input a word:";
		cin>>word;
		int n=0,n1,n2;
		f2<<s;//取出f2中的单串文件字符串
		while(!f2.eof())
		{
			if(s.find(word,n)!=string::npos)
			{
				n=s.find(word,n);//找到那个单词所在的位置 
				n1=s.rfind("-",n);//找到单词前面的“-” 
				n2=s.find("-",n);// 找到单词后面的“-”
				n=s.find(" ",n);// 位置移到把单词后面的空格,目的是用来取出单词
				if(s.substr(n1+1,n-n1-1)==word) 
				{
					cout<<s.substr(n1+1,n2-n1-1)<<endl ;
					break;
				}
			}
		}
	} 
	~Dict()
	{
		f1.close();
		f2.close();
	}
};
int main()
{
	Dict d;
	d.find_w();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值