C++电子词典程序中的小问题:文件传输

dg1234567 2009-05-15 10:59:36
关于用c++写的一个模拟电子词典的程序

有这样的一个形式的词典文件:
皮格马利翁 ||| Pygmalion
爱偶像癖 ||| Pygmalionism
派尔 ||| Pyle
派卢明浸镀铬酸盐法 ||| Pylumin
派卢明铬酸氧化处理法 ||| Pylumin
皮姆 ||| Pym
平壤 ||| Pyongyang
田乐磷 ||| Pyracide
杀草敏 ||| Pyrazon
定菌磷 ||| Pyrazophos
比利牛斯山脉 ||| Pyrenean
比利牛斯山脉 ||| Pyrenees
耐热玻璃 ||| Pyrex
其商标名 ||| Pyrex
嘧啶磷 ||| Pyrimithate
派罗卡斯特铁铬耐热合金 ||| Pyrocast
耐高温陶瓷 ||| Pyroceram
皮罗依 ||| Pyroil
派罗利克高电阻镍铬合金 ||| Pyrolic
派罗梅特镍基耐热合金 ||| Pyromet
一种镍铬耐热合金 ||| Pyromic
派朗 ||| Pyron

现在我用一个word结构体 分别含有 char english[20] char chinese[30] 两个变量用于存储读入的内容
int find(char aim[], word &w)
{
int index=0;
io_file.seekg(0);
io_file>>w.chinese>>w.english;
while (!io_file.eof() )
{
if (strcmp(w.english,aim) == 0)
{
cout<<w.english<<" "<<w.chinese<<endl;
return index;
}
index++;
io_file>>w.chinese>>w.english;
}
return -1;

}

但是读入不了,请大家帮忙指点一下 读取文件中的单词录入结构体的时候该怎么读?用什么函数?

...全文
239 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lori227 2009-05-16
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Word_
{
friend istream& operator >> (istream &ris, Word_ &word); //重载输入操作
string english;
string chinese;
};

istream& operator >> (istream &ris, Word_ &word)
{
string strValue;
getline(ris, strValue); //read line

word.chinese.clear();
word.english.clear();

if (!strValue.empty())
{
string::size_type pos = strValue.find(" ||| ");
if (pos != string::npos)
{
word.chinese = strValue.substr(0, pos);

word.english = strValue.substr(pos + 5, strValue.size());
//要去除后面的空格
if (word.english.at(word.english.size() - 1) == ' ')
{
word.english = word.english.substr(0, word.english.size() - 1);
}
}
}

return ris;
}

void Print(const Word_ &word)
{
cout << word.chinese << "\t" << word.english << "\n";
}

Word_* Find(vector<Word_> &vecWord, string english)
{
if (vecWord.empty() || english.empty())
{
return NULL;
}

vector<Word_>::iterator iter;
vector<Word_>::iterator begin = vecWord.begin();
vector<Word_>::iterator end = vecWord.end();
for (iter = begin; iter != end; ++iter)
{
if (english == (*iter).english)
{
return &(*iter);
}
}

return NULL;
}

int _tmain(int argc, _TCHAR* argv[])
{

ifstream infile;
infile.open("D:\\test.txt");
if (!infile)
{
return 0;
}

vector<Word_> vecWord; //保存结构的容器
Word_ stword;

while (infile >> stword, !infile.eof()) //读文件
{
if (!stword.chinese.empty())
{
vecWord.push_back(stword);
}
}

//打印信息
for_each(vecWord.begin(), vecWord.end(), Print);

cout << "\n查找 :" ;
string english;
cin >> english;
//查找
Word_ *pword = Find(vecWord, english);

if (pword != NULL)
{
cout << "\n" << pword->chinese << "\t" << pword->english << endl;
}


return 0;
}
dg1234567 2009-05-15
  • 打赏
  • 举报
回复
文件可以打开,但是会终止
想问一下如果用二进制去读的话是不是好一点呢?
aaaa3105563 2009-05-15
  • 打赏
  • 举报
回复
呵呵 帮顶··
mengde007 2009-05-15
  • 打赏
  • 举报
回复
文件没打开?
shexinwei 2009-05-15
  • 打赏
  • 举报
回复
直接不可以读吗??
先读结构体中第一个也就是英文 判断“|||”,后面继续读结构体的第二部分

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧