问题描述
所以,我目前正试图弄清楚如何打开一个包含大量不同大小的不同单词的文件。然后在文件中搜索一定大小的所有单词,读入一个数组。然后将数组中的所有单词写入屏幕并反向写入新文件。假设原文件有A开头的单词,我创建的新文件应该有Z开头的单词。
这是我现在拥有的代码。
cout << "What size word do you want? ";
cin >> size;
//INput must be between 1 and 30
while (size > 30 || size < 1)
{
cout << "Please enter a number between 1 and 30\n";
cout << "Try again: ";
cin >> size;
}
ifstream inputFile;
ofstream outputFile;
string* arrPtr;
arrPtr = new string[count]; //I got count from a different part of the code that counts how many //words there are of that size. Because I also have to display how many words of the size I chose there //are.
int j = 0;
inputFile.open("WordList.txt");
outputFile.open("WordList2.txt");
while (getline(inputFile,arrPtr[j]))
{
inputFile >> arrPtr[j];
if (arrPtr[j].length() == size)
{
outputFile << arrPtr[j];
cout << arrPtr[j] << " ";
}
}
cout << endl;
outputFile.close();
inputFile.close();
delete[] arrPtr;
目前为止可以找到一定大小的单词,打印出来存入文件中。 但是,我无法弄清楚如何将每个单词放在文件中的不同行,或者如何向后读取和存储单词。此外,当我打印出单词时,它似乎跳过了文件中大小合适的第一个单词。 如果我能得到一些帮助,那将不胜感激。谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)