C++ 程序错误地从文本文件中读取信息,看起来是从左到右,然后是从右到左
我试图读入关于A.A单词和B.所说单词的频率的信息。当函数读入信息时,第一个调用从左(Word)到右(Frequency)读取信息,然后下一个调用从右(Frequency)到左(Word)。我假设这是由于我的格式。我的假设是它跳过了这个词C++ 程序错误地从文本文件中读取信息,看起来是从左到右,然后是从右到左,c++,fstream,C++,Fstream,我试图读入关于A.A单词和B.所说单词的频率的信息。当函数读入信息时,第一个调用从左(Word)到右(Frequency)读取信息,然后下一个调用从右(Frequency)到左(Word)。我假设这是由于我的格式。我的假设是它跳过了这个词 dicFile>word>>freq; intfrequ=stoi(freq); newItem.key=word; newItem.wordCount=frequ; 树。AVL_插入(新项); } } 否则{cout那是因为它不能处理“苹果” 首先,whil
dicFile>word>>freq;
intfrequ=stoi(freq);
newItem.key=word;
newItem.wordCount=frequ;
树。AVL_插入(新项);
}
}
否则{cout那是因为它不能处理“苹果”
首先,while(dictionaryFile>>word)
读“Accounting”,然后
dictionaryFile >> word >> freq;
将“3”读入word
,将“苹果”读入freq
你应该做的是
while (dictionaryFile >> word >> freq) {
int frequ = stoi(freq);
// ...
或者,您可以删除一组变量无序排列,并直接读入“项”:
while(dictionaryFile>>word){dictionaryFile>>word>>freq;
->你读了两遍word
,然后又读了两遍freq
。根据你的描述,这不是你想要做的。。。(通过在调试器中逐步执行来检查这一点。查看word
和freq
中在while
中的第一行之后出现的内容,谢谢你。IMHO,正确的方法是while(dictionaryFile>>word>>freq){
然后完全删除下一行。(不客气)。;-)您也可以考虑创建<代码> Frq<代码>类型<代码> int <代码>。这将使<代码> STOIE()<代码>过时。
while (dictionaryFile >> newItem.key >> newItem.wordCount) {
tree.AVL_Insert(newItem);
}