#include <fstream.h>
#include <string.h>
#include <iostream.h>
//讀文件用到的方法
fstream file;
file.open("data.txt",ios::in);//以讀方式打開文件data.txt
file.eof() 判斷文件是否讀完
file.getline(char *line, int n)
getline方法用於從文件裏讀取壹行字符。其中,第壹個參數line是壹個指向字符串的字符指針或存放字符串的字符數組,n表示本次讀取的最大字符個數。
//寫文件用到的方法
file.open("data.txt",ios::out);//以寫方式打開文件data.txt
file.write(char *line, int n)
write方法用於向文件寫壹行字符。其中,第壹個參數line是壹個指向字符串的字符指針或存放字符串的字符數組,n表示本次寫的字符個數。
file.put(char c), put方法用於向文件寫壹個字符。
file.close();//關閉文件 ,不論讀或寫文件,使用完後需要關閉文件。
程序如下,經過修改,顯示沒有錯誤和警告。但是運行時查找,插入,刪除單詞時就卡住了。請高人指點,到底是怎麽回事?謝謝了!
dictionary.h
#ifndef _DICTIONARY_H
#define _DICTIONARY_H
const int size=30;
/*******************公有部分***********************/
dictionary::dictionary()//如果記錄為空,創建壹個*作為結尾
{
text.open("d:\\tiny-dict.txt",ios::in|ios::out);
if(text.eof())
text<<"* ";
text.close();
}
void dictionary::findWord()
{
cout<<"請輸入所要查詢的單詞."<<endl;
string word;
cin>>word;
text.open("d:\\tiny-dict.txt",ios::in);//打開壹個文件作讀操作
int i=0;
while(1)
{
string temp;
text>>temp;
if(temp=="*")//在數據的末尾加上*表示文件結束
{
cout<<"未找到該單詞."<<endl;
break;
}
if(word==temp)
{
cout<<"釋義:";
do
{
text>>temp;
if(temp=="%")//在釋義末尾加上%表示釋義結束
break;
cout<<temp<<" ";
}while(temp!="%");
cout<<endl;
}
if(temp=="%")
break;
i+=size;
text.seekg(i,ios::beg);
}
text.close();
}
void dictionary::insertWord()
{
cout<<"輸入妳所要添加的單詞."<<endl;
string word;
cin>>word;
cout<<"輸入妳添加單詞的釋義."<<endl;
string paraphrase;
cin.ignore();
getline(cin,paraphrase);
cout<<paraphrase<<endl;
text.open("d:\\tiny-dict.txt",ios::in|ios::out);
char z;
int i=-size;
while(z!='*'&&z!='+')//*用來標識結尾,+用來表示刪除壹個單詞留下的區域
{
i+=size;
text.seekp(i);
text>>z;
}
if(word.length()+paraphrase.length()+4>size)//單詞與釋義之間壹個空格和用於標識結尾的空格加%加空格所以加4
{
cout<<"輸入太長,插入失敗."<<endl;
return ;
}
text.seekp(i,ios::beg);
text<<word<<" "<<paraphrase<<" % ";
if(z=='+')
{
text.close();
return ;
}
text.seekp(i+size,ios::beg);
text<<"* ";
text.close();
}
void dictionary::deleteWord()
{
cout<<"請輸入刪除的單詞."<<endl;
string word;
cin>>word;
text.open("d:\\tiny-dict.txt",ios::in|ios::out);
string Deleted;
int i=-size;//標記刪除的位置
do
{
i+=size;
text.seekg(i);
text>>Deleted;
if(Deleted=="*")
{
cout<<"未發現該單詞,刪除失敗"<<endl;
return ;
}
}while(Deleted!=word);
text.seekp(i);
text<<"+ ";//第壹元素標記為+,表示空
for(int j=1;j<size;j++)
{
text<<" ";
}
cout<<"刪除成功."<<endl;
text.close();
}
#endif
main.cpp
using std::cout;
using std::endl;
using std::cin;
#include<fstream>
using std::ios;
using std::fstream;
#include<string>
using std::string;
using std::getline;
#include"dictionary.h"
class dictionary
{
public:
dictionary();
void findWord();
void insertWord();
void deleteWord();
private:
fstream text;
};
void instruction()
{
cout<<"請選擇:\n(1).查找單詞\n(2).插入單詞\n(3).刪除單詞\n(4).退出\n";
}
int main()
{
instruction();
int select=0;
cin>>select;
while(select!=1&&select!=2&&select!=3&&select!=4)
{
cout<<"選擇錯誤,請重新選擇."<<endl;
instruction();
cin>>select;
}
dictionary oper;
bool exe=true;
while(exe)
{
switch(select)
{
case 1:
oper.findWord();
break;
case 2:
oper.insertWord();
break;
case 3:
oper.deleteWord();
break;
case 4:
exe=false;
break;
}
if(select==4)
break;
instruction();
cin>>select;
while(select!=1&&select!=2&&select!=3&&select!=4)
{
cout<<"選擇錯誤,請重新選擇."<<endl;
instruction();
cin>>select;
}
}
return 0;
}
d:\\tiny-dict.txt 裏的內容如下:(是個英德字典,左邊壹列是英語,右邊是德語)
bird Raubvogel, Greifvogel
rabbit Kaninchen
monkey Affe
horse Pferd
crab Krabbe, Krebs
eagle Adler, Aar; Zehn-Dollar-Note