C++ 如何中断读取此文本文件的过程(使用ifstream)?C++;
当“同义词”出现时,我想停止读取文本输入文件。我正在使用ifstream,我不知道如何打破循环。我尝试使用stringstream“同义词”,但它最终废弃了我的bst。我在下面列出了完整的项目文件,以防你想避免键入 重要部分:C++ 如何中断读取此文本文件的过程(使用ifstream)?C++;,c++,ifstream,C++,Ifstream,当“同义词”出现时,我想停止读取文本输入文件。我正在使用ifstream,我不知道如何打破循环。我尝试使用stringstream“同义词”,但它最终废弃了我的bst。我在下面列出了完整的项目文件,以防你想避免键入 重要部分: for(;;) /*here, I wanna break the cycle when it reads "synonyms"*/ { inStream >> word;
for(;;) /*here, I wanna break the cycle when it reads "synonyms"*/
{
inStream >> word;
if (inStream.eof()) break;
wordTree.insert(word);
}
wordTree.graph(cout);
字典.txt
1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3
Project.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "MiBST.h"
using namespace std;
class WordInfo{
public:
//--id accesor
int id ()const {return myId; }
/* myId is the number that identifies each word*/
//--input function
void read (istream &in)
{
in>>myId>>word;
}
//--output function
void print(ostream &out)
{
out<<myId<<" "<<word;
}
//--- equals operator
bool operator==(const WordInfo & otherword) const
{ return myId == otherword.myId; }
//--- less-than operator
bool operator<(const WordInfo & otherword) const
{ return myId < otherword.myId; }
private:
int myId;
string word;
};
//--- Definition of input operator
istream & operator>>(istream & in, WordInfo & word)
{
word.read(in);
}
//---Definition of output operator
ostream & operator <<(ostream &out, WordInfo &word)
{
word.print(out);
}
int main(){
// Open stream to file of ids and words
string wordFile;
cout << "Enter name of dictionary file: ";
getline(cin, wordFile);
ifstream inStream(wordFile.data());
if (!inStream.is_open())
{
cerr << "Cannot open " << wordFile << "\n";
exit(1);
}
// Build the BST of word records
BST<WordInfo> wordTree; // BST of word records
WordInfo word; // a word record
for(;;) /*here, I wanna break the cycle when it reads "synonyms"*/
{
inStream >> word;
if (inStream.eof()) break;
wordTree.insert(word);
}
wordTree.graph(cout);
//wordTree.inorder(cout);
system ("PAUSE");
return 0;
}
#包括
#包括
#包括
#包括
#包括“MiBST.h”
使用名称空间std;
类WordInfo{
公众:
//--身份证持有人
int id()常量{return myId;}
/*myId是标识每个单词的数字*/
//--输入函数
无效读取(istream&in)
{
在>>myId>>word中;
}
//--输出函数
作废打印(ostream&out)
{
outdata)//插入到父对象的左侧
父->左=locptr;
else//插入到父级的右侧
父->右=locptr;
}
其他的
std::cout right!=0)
{//节点有2个子节点
//查找x的顺序继承者及其父级
typename BST::BinNodePointInter xSucc=x->右侧;
父代=x;
而(xSucc->left!=0)//向左下降
{
父项=xSucc;
xSucc=xSucc->左;
}
//将xSucc的内容移动到x并更改x
//指向将被删除的继任者。
x->data=xSucc->data;
x=xSucc;
}//如果节点有2个子节点,则结束
//现在继续处理节点有0或2个子节点的情况
类型名BST::BinNodePointInter
subtree=x->left;//指向x子树的指针
如果(子树==0)
子树=x->右;
if(parent==0)//正在删除根
myRoot=子树;
else if(parent->left==x)//父对象的左子对象
父->左=子树;
else//父母的右子女
父->右=子树;
删除x;
}
//---inoder()的定义
模板
内联void BST::索引(std::ostream&out)常量
{
inorderAux(out,myRoot);
}
//---图()的定义
模板
内联void BST::graph(std::ostream&out)常量
{graphAux(out,0,myRoot);}
//---search2()的定义
模板
void BST::search2(常量数据类型和项、bool和found、,
英国夏令时:BinNodePointer和locptr,
BST::BinNodePointInter和parent)常量
{
locptr=myRoot;
父项=0;
发现=错误;
而(!found&&locptr!=0)
{
如果(项目data)//向左下降
{
父代=locptr;
locptr=locptr->左;
}
else if(locptr->data- 右侧;
}
else//找到项目
发现=真;
}
}
//---inorderAux()的定义
模板
无效BST::inorderAux(标准::ostream&out,
BST::BinNodePointInter子树根)常量
{
if(subtreeRoot!=0)
{
inorderAux(out,subtreroot->left);//L操作
out data right);//R操作
}
}
//---graphAux()的定义
模板
无效BST::图形(标准::ostream&out,整数缩进,
BST::BinNodePointInter子树根)常量
{
if(subtreeRoot!=0)
{
图形(向外,缩进+8,次根->右);
out您应该在WordInfo上设置一个运算符==以将其与字符串进行比较,然后您可以在读取循环中执行以下操作:
if ( word == "synonyms" ) break;
您应该在WordInfo上设置运算符==以将其与字符串进行比较,然后您可以在读取循环中执行以下操作:
if ( word == "synonyms" ) break;
你可以这样做
/* here, it stops when reading "synonyms" or when failing to extract a word. */
while(inStream >> word && word != "synonym") {
wordTree.insert(word);
}
wordTree.graph(cout);
请注意,当它无法读取非空白字符序列时,它会设置流的失败位。然后,inStream的计算结果为false。这就是循环工作的原因。使用.eof()时要小心
只有在您尝试读取文件结尾以外的内容后,才会返回true。例如,您将退出循环,并错过此处的单词3树:
假设树后没有空格。如果单词信息之间有换行符,并且最后一个单词后面没有尾随换行符,则当然会发生同样的情况。使用if(inStream)
(while循环隐式执行此操作)是安全的。在这种情况下,它的计算结果仍然为true,只有在它没有读取除空格以外的任何内容时才为false。您可以这样做
/* here, it stops when reading "synonyms" or when failing to extract a word. */
while(inStream >> word && word != "synonym") {
wordTree.insert(word);
}
wordTree.graph(cout);
请注意,当它无法读取非空白字符序列时,它会设置流的失败位。然后,inStream的计算结果为false。这就是循环工作的原因。使用.eof()时要小心
只有在您尝试读取文件结尾以外的内容后,才会返回true。例如,您将退出循环,并错过此处的单词3树:
假设树后没有空格。如果单词信息之间有换行符,并且最后一个单词后面没有尾随换行符,则当然会发生同样的情况。使用if(inStream)
(while循环隐式执行此操作)是安全的。在这种情况下,它仍将计算为true,只有在除空格外未读取任何内容时才计算为false。@SoapBox:
我创建了这个==运算符:
//--- equals operator for String
bool operator==(const string & aString) const
{ return word == aString; } // word is the WordInfo string field for 'real' word
并将(;;)的更改为:
结果是无限循环打印:
"Item already in the tree"
顺便说一句,我之前用这样一个示例字典测试了这棵树,它成功了
dict2.txt
1可爱
2你好
三丑
4轻松
5困难
6累了
7美丽的肥皂盒:
我创建了这个==运算符:
//--- equals operator for String
bool operator==(const string & aString) const
{ return word == aString; } // word is the WordInfo string field for 'real' word
并将(;;)的更改为:
结果是无限循环打印:
"Item already in the tree"
顺便说一句,我之前用这样一个示例字典测试了这棵树,它成功了
dict2.txt
1可爱
2你好
三丑
4轻松
5困难
6累了
7漂亮