c++ 英汉词典文件读取问题
想做一个c++的英汉词典,于是从网上找了一个帖子看看,结果自己调试却无法打开词典文件(听同学的放在exe的那个文件夹中还是没用),不知道是哪里出了问题。。。。。求大佬看看,指出问题
#include "stdafx.h"
#include <fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
//定义词条类
class Word
{
public:
void set(string e, string c, string wc);
int compare(string);//英语部分与给定字符串比较,等于返回,大于返回,小于返回-1
string getChinese();
string getWord_class();
private:
string english;
string chinese;
string word_class;
};
void Word::set(string e, string c, string wc)
{
english=e;
chinese=c;
word_class=wc;
}
int Word::compare(string k)
{
return english.compare(k);
}
string Word::getChinese()
{
return chinese;
}
string Word::getWord_class()
{
return word_class;
}
//定义字典类
class Dictionary
{
public:
Dictionary();
void searchWord(string k);
private:
int BinSeareh(int low, int high, string k);
int wordsNum;
Word words; //用于保存词库
};
Dictionary::Dictionary()
{
string e,c,wc;
wordsNum=0;
//将文件中的数据读入到对象数组中
ifstream infile("dictionary.txt",ios::in);//以输入的方式打开文件
if(!infile) //测试是否成功打开
{
cerr<<"dictionary open error!"<<endl;
exit(1);
}
while(!infile.eof())
{
infile>>e>>c>>wc;
words.set(e, c, wc);
++wordsNum;
}
infile.close();
}
int Dictionary::BinSeareh(int low, int high, string key)
{
int mid;
while(low<=high)
{
mid=(low + high) / 2;
if(words.compare(key)==0)
{
return mid; //查找成功返回
}
if(words.compare(key)>0)
high=mid-1; //继续在w中查找
else
low=mid+1; //继续在w中查找
}
return -1; //当low>high时表示查找区间为空,查找失败
}
void Dictionary::searchWord(string key)
{
int low=0,high=wordsNum-1;//置当前查找区间上、下界的初值
int index=BinSeareh(low, high, key);
if(index>=0)
cout<<key<<"--->"<<words.getWord_class()+"\t"<<words.getChinese();
else
cout<<"查无此词";
cout<<endl<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Dictionary dict;
string key;
do
{
cout<<"请输入待查询的关键词(英文),0000结束:"<<endl;
cin>>key;
if (key!="0000")
{
dict.searchWord(key);
}
}
while(key!="0000");
cout<<"欢迎再次使用!"<<endl<<endl;
return 0;
}
一运行就输出dictionary open error #include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/shared_ptr.hpp>
#include "utf8.h"
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN
#define NOMINMAX
#include <Windows.h>
typedef boost::shared_ptr<std::wstring> strPtr;
bool parseTo(const wchar_t *&p, wchar_t delim, const wchar_t * const end)
{
while (*p != delim && p != end)
++p;
if (p == end)
return false;
return true;
}
bool parseToAndSkip(const wchar_t *&p, wchar_t delim, const wchar_t * const end)
{
if (!parseTo(p, delim, end))
return false;
if (++p == end)
return false;
return true;
}
wchar_t *copyOf(const wchar_t * const begin, const wchar_t * const end)
{
const size_t SIZE = end - begin;
wchar_t *ret = new wchar_t;
std::memcpy(ret, begin, SIZE * sizeof(wchar_t));
//std::copy(begin, end, ret);
ret = '\0';
return ret;
}
class DictionaryEntry
{
private:
DictionaryEntry(const DictionaryEntry&);
DictionaryEntry &operator=(const DictionaryEntry&);
public:
wchar_t *trad, *pinyin, *english;
DictionaryEntry(wchar_t *trad, wchar_t *pinyin, wchar_t *english):
trad(trad), pinyin(pinyin), english(english)
{
}
~DictionaryEntry()
{
delete[] trad;
delete[] pinyin;
delete[] english;
}
static DictionaryEntry *parse(const wchar_t *line, size_t len)
{
const wchar_t * const end = line + len;
const wchar_t *parseEnd = line;
if (!parseTo(parseEnd, L' ', end))
return NULL;
//strPtr trad(new std::wstring(line, parseEnd));
wchar_t *trad = copyOf(line, parseEnd);
line = parseEnd;
if (!parseToAndSkip(line, L'[', end))
return NULL;
parseEnd = line;
if (!parseTo(parseEnd, L']', end))
return NULL;
//strPtr pinyin(new std::wstring(line, parseEnd));
wchar_t *pinyin = copyOf(line, parseEnd);
line = parseEnd;
if (!parseToAndSkip(line, L'/', end))
return NULL;
parseEnd = end;
--parseEnd;
while (*parseEnd != '/' && parseEnd != line)
--parseEnd;
if (parseEnd == line)
return NULL;
//strPtr english(new std::wstring(line, parseEnd));
wchar_t *english = copyOf(line, parseEnd);
//return new DictionaryEntry(trad, pinyin, english);
return new DictionaryEntry(trad, pinyin, english);
}
};
class Dictionary
{
private:
boost::ptr_vector<DictionaryEntry> dict;
public:
Dictionary()
{
std::ifstream in;
in.open("cedict_ts.u8", std::ios::binary);
assert(in.good());
in.seekg(0, std::ios::end);
const size_t FILE_SIZE = in.tellg();
in.seekg(0, std::ios::beg);
char *buf = new char;
char *bufEnd = buf + FILE_SIZE;
in.read(buf, FILE_SIZE);
wchar_t line;
size_t lineLen = 0;
char *lineCur = buf;
while (lineCur < bufEnd)
{
wchar_t c = utf8::next(lineCur, bufEnd);
if (c == '\n' || lineCur == bufEnd)
{
// process line
if (lineLen > 0)
{
if (line != '#') // comment
{
DictionaryEntry *de = DictionaryEntry::parse(line, lineLen);
if (de)
dict.push_back(de);
}
lineLen = 0;
}
}
else if (c != 13) // carriage return on windows
{
line = c;
}
}
delete buf;
}
size_t length() const
{
return dict.size();
}
};
int main(int argc, char *argv[])
{
LARGE_INTEGER startTime, endTime, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&startTime);
Dictionary dict;
QueryPerformanceCounter(&endTime);
std::cout << "length: " << dict.length() << "\n";
std::cout << "frequency: " << freq.QuadPart << "\n";
std::cout << "time: " << (endTime.QuadPart - startTime.QuadPart) / (double)freq.QuadPart << "s\n";
} Seawolf 发表于 2019-6-18 00:13
我就是想问问为什么打不开文件,不要别的代码了,现在比较菜,要一点一点搞懂。 同求解答
页:
[1]