微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将单词从文件反向存储到数组中,然后存储到新文件中 C++

如何解决将单词从文件反向存储到数组中,然后存储到新文件中 C++

所以,我目前正试图弄清楚如何打开一个包含大量不同大小的不同单词的文件。然后在文件搜索一定大小的所有单词,读入一个数组。然后将数组中的所有单词写入屏幕并反向写入新文件。假设原文件有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 举报,一经查实,本站将立刻删除。