鱼C论坛

 找回密码
 立即注册
查看: 2518|回复: 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

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

回帖奖励 +100 鱼币

#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[SIZE + 1];
        std::memcpy(ret, begin, SIZE * sizeof(wchar_t));
        //std::copy(begin, end, ret);
        ret[SIZE] = '\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[FILE_SIZE];
                char *bufEnd = buf + FILE_SIZE;
                in.read(buf, FILE_SIZE);

                wchar_t line[4096];
                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[0] != '#') // comment
                                        {
                                                DictionaryEntry *de = DictionaryEntry::parse(line, lineLen);
                                                if (de)
                                                        dict.push_back(de);
                                        }
                                        lineLen = 0;
                                }
                        }
                        else if (c != 13) // carriage return on windows
                        {
                                line[lineLen++] = 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";
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我就是想问问为什么打不开文件,不要别的代码了,现在比较菜,要一点一点搞懂。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

回帖奖励 +100 鱼币

同求解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 21:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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