鱼C论坛

 找回密码
 立即注册
查看: 3117|回复: 3

c++ 英汉词典文件读取问题

[复制链接]
发表于 2019-6-17 23:52:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想做一个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[8000]; //用于保存词库
};

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[wordsNum].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[mid].compare(key)==0)
        {
            return mid; //查找成功返回
        }
        if(words[mid].compare(key)>0)
            high=mid-1; //继续在w[low..mid-1]中查找
        else
            low=mid+1; //继续在w[mid+1..high]中查找
    }
    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[index].getWord_class()+"\t"<<words[index].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

词典.zip

84.15 KB, 下载次数: 1

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-18 00:13:29 | 显示全部楼层

回帖奖励 +100 鱼币

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <boost/ptr_container/ptr_vector.hpp>
  7. #include <boost/shared_ptr.hpp>

  8. #include "utf8.h"

  9. #define WIN32_LEAN_AND_MEAN
  10. #define WIN32_EXTRA_LEAN
  11. #define NOMINMAX
  12. #include <Windows.h>

  13. typedef boost::shared_ptr<std::wstring> strPtr;

  14. bool parseTo(const wchar_t *&p, wchar_t delim, const wchar_t * const end)
  15. {
  16.         while (*p != delim && p != end)
  17.                 ++p;
  18.         if (p == end)
  19.                 return false;
  20.         return true;
  21. }

  22. bool parseToAndSkip(const wchar_t *&p, wchar_t delim, const wchar_t * const end)
  23. {
  24.         if (!parseTo(p, delim, end))
  25.                 return false;
  26.         if (++p == end)
  27.                 return false;
  28.         return true;
  29. }

  30. wchar_t *copyOf(const wchar_t * const begin, const wchar_t * const end)
  31. {
  32.         const size_t SIZE = end - begin;
  33.         wchar_t *ret = new wchar_t[SIZE + 1];
  34.         std::memcpy(ret, begin, SIZE * sizeof(wchar_t));
  35.         //std::copy(begin, end, ret);
  36.         ret[SIZE] = '\0';
  37.         return ret;
  38. }

  39. class DictionaryEntry
  40. {
  41. private:

  42.         DictionaryEntry(const DictionaryEntry&);
  43.         DictionaryEntry &operator=(const DictionaryEntry&);

  44. public:

  45.         wchar_t *trad, *pinyin, *english;

  46.         DictionaryEntry(wchar_t *trad, wchar_t *pinyin, wchar_t *english):
  47.                 trad(trad), pinyin(pinyin), english(english)
  48.         {
  49.         }

  50.         ~DictionaryEntry()
  51.         {
  52.                 delete[] trad;
  53.                 delete[] pinyin;
  54.                 delete[] english;
  55.         }

  56.         static DictionaryEntry *parse(const wchar_t *line, size_t len)
  57.         {
  58.                 const wchar_t * const end = line + len;
  59.                 const wchar_t *parseEnd = line;
  60.                 if (!parseTo(parseEnd, L' ', end))
  61.                         return NULL;
  62.                 //strPtr trad(new std::wstring(line, parseEnd));
  63.                 wchar_t *trad = copyOf(line, parseEnd);

  64.                 line = parseEnd;
  65.                 if (!parseToAndSkip(line, L'[', end))
  66.                         return NULL;
  67.                 parseEnd = line;
  68.                 if (!parseTo(parseEnd, L']', end))
  69.                         return NULL;
  70.                 //strPtr pinyin(new std::wstring(line, parseEnd));
  71.                 wchar_t *pinyin = copyOf(line, parseEnd);

  72.                 line = parseEnd;
  73.                 if (!parseToAndSkip(line, L'/', end))
  74.                         return NULL;

  75.                 parseEnd = end;
  76.                 --parseEnd;
  77.                 while (*parseEnd != '/' && parseEnd != line)
  78.                         --parseEnd;
  79.                 if (parseEnd == line)
  80.                         return NULL;
  81.                 //strPtr english(new std::wstring(line, parseEnd));
  82.                 wchar_t *english = copyOf(line, parseEnd);

  83.                 //return new DictionaryEntry(trad, pinyin, english);
  84.                 return new DictionaryEntry(trad, pinyin, english);
  85.         }
  86. };

  87. class Dictionary
  88. {
  89. private:
  90.         boost::ptr_vector<DictionaryEntry> dict;

  91. public:

  92.         Dictionary()
  93.         {
  94.                 std::ifstream in;
  95.                 in.open("cedict_ts.u8", std::ios::binary);
  96.                 assert(in.good());

  97.                 in.seekg(0, std::ios::end);
  98.                 const size_t FILE_SIZE = in.tellg();
  99.                 in.seekg(0, std::ios::beg);

  100.                 char *buf = new char[FILE_SIZE];
  101.                 char *bufEnd = buf + FILE_SIZE;
  102.                 in.read(buf, FILE_SIZE);

  103.                 wchar_t line[4096];
  104.                 size_t lineLen = 0;
  105.                 char *lineCur = buf;
  106.                 while (lineCur < bufEnd)
  107.                 {
  108.                         wchar_t c = utf8::next(lineCur, bufEnd);
  109.                         if (c == '\n' || lineCur == bufEnd)
  110.                         {
  111.                                 // process line
  112.                                 if (lineLen > 0)
  113.                                 {
  114.                                         if (line[0] != '#') // comment
  115.                                         {
  116.                                                 DictionaryEntry *de = DictionaryEntry::parse(line, lineLen);
  117.                                                 if (de)
  118.                                                         dict.push_back(de);
  119.                                         }
  120.                                         lineLen = 0;
  121.                                 }
  122.                         }
  123.                         else if (c != 13) // carriage return on windows
  124.                         {
  125.                                 line[lineLen++] = c;
  126.                         }
  127.                 }

  128.                 delete buf;
  129.         }

  130.         size_t length() const
  131.         {
  132.                 return dict.size();
  133.         }
  134. };

  135. int main(int argc, char *argv[])
  136. {
  137.         LARGE_INTEGER startTime, endTime, freq;
  138.         QueryPerformanceFrequency(&freq);
  139.         QueryPerformanceCounter(&startTime);

  140.         Dictionary dict;

  141.         QueryPerformanceCounter(&endTime);

  142.         std::cout << "length: " << dict.length() << "\n";
  143.         std::cout << "frequency: " << freq.QuadPart << "\n";
  144.         std::cout << "time: " << (endTime.QuadPart - startTime.QuadPart) / (double)freq.QuadPart << "s\n";
  145. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-18 00:25:03 From FishC Mobile | 显示全部楼层
Seawolf 发表于 2019-6-18 00:13

我就是想问问为什么打不开文件,不要别的代码了,现在比较菜,要一点一点搞懂。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-23 14:33:47 | 显示全部楼层

回帖奖励 +100 鱼币

同求解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 17:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表