Другие языки программирования и технологии

Как считать слова из текстового файла в с++?

Причём так, чтобы можно было потом их записать в обратном порядке в другой файл?
#include <string>
#include <vector>
#include <fstream>
#include <iterator>

using namespace std;

int main() {
    ifstream in("in.txt");
    vector<string> words;
    string word;
    while (in >> word) words.push_back(word);

    ofstream out("out.txt");
    reverse_iterator<vector<string>::iterator> it(words.end());
    reverse_iterator<vector<string>::iterator> end(words.begin());
    for (; it != end; ++it) out << *it << ' ';
}

Или:

#include <string>
#include <fstream>

using namespace std;

void revout(ifstream &in, ofstream &out) {
    string word;
    if (in >> word) {
        revout(in, out);
        out << word << ' ';
    }
}

int main() {
    revout(ifstream("in.txt"), ofstream("out.txt"));
}

Или:

#include <stack>
#include <string>
#include <fstream>

using namespace std;

int main() {
    ifstream in("in.txt");
    stack<string> words;
    string word;
    while (in >> word) words.push(word);

    ofstream out("out.txt");
    while ( !words.empty() ) {
        out << words.top() << ' ';
        words.pop();
    }
}

Или:
Можно придумать еще пару-тройку способов. В общем, учитесь, и все будет получаться. Читайте нормальную литературу, вроде
Максим Никитин
Максим Никитин
52 539
Лучший ответ
читаешь в массив и пишешь в др. файл в обратном порядке.
Михаил Логинов
Михаил Логинов
42 602