文件操作举例


#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<vector>
#define MAX 50
#define MAXN 20
using namespace std;

typedef struct{
	char word[MAXN];
	char explain[MAX];
}Word;

vector<Word>  my_word_book; 
vector<Word>::iterator it1;

void build_word();
void Search(char* word_look,vector<Word> my_book);
void Search1(char* word_look,vector<Word> my_book);
bool Check(char* word1, char* word2);

int main()
{
	char word[MAXN];

	build_word();//建单词本 
	int t;
	
	cout<<"英译汉 请输入1,汉译英 请输入2:"<<endl;
	cin>>t; 
	cout<<"input the word you want to search:  "<<endl;
	cin>>word;
	if(t==1){
	Search(word,my_word_book);
    }  
	else 
	Search1(word,my_word_book);
	
	return 0;
}
//建立单词库
void build_word()
{
	ifstream txtfile;//建立文件流 
	
	txtfile.open("C:\\Users\\lenovo\\Desktop\\单词库.txt");//打开文件,读取数据 
	//判断文件是否打开正确 
	if(!txtfile)
	{
		cerr<<"File open error!"<<endl;
		exit(-1);
	}
	int i=0;
	Word w1,w2;
	
	while(!txtfile.eof())
	{
		txtfile>>w1.word>>w1.explain;//依次读入每个单词和解释 
		
		my_word_book.push_back(w1); //压入vector中 
	}
	txtfile.close();//关闭文件 

} 

void Search(char* word,vector<Word> my_book)
{
	for(it1=my_book.begin();it1!=my_book.end();it1++)
	{
		if(strcmp(it1->word,word)==0)
		{
		cout<<it1->explain<<endl;
		exit(-1);
		}
	}
		cout<<"查询不到该单词!"<<endl; 
	//cout<<"查询不到该单词!"<<endl; 
}

void Search1(char* word, vector<Word>my_book){
	
	for(it1=my_book.begin();it1!=my_book.end()-1;it1++){
		if(Check(word,it1->explain)){
		cout<<it1->word<<endl;
		}
	}

	
}

bool Check(char* word1, char *word2){
	if(strstr(word2,word1)){
		return true;
	}
	else
		return false;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值