Другие языки программирования и технологии
Как считать слова из текстового файла в с++?
Причём так, чтобы можно было потом их записать в обратном порядке в другой файл?
#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();
}
}
Или:
Можно придумать еще пару-тройку способов. В общем, учитесь, и все будет получаться. Читайте нормальную литературу, вроде

#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();
}
}
Или:
Можно придумать еще пару-тройку способов. В общем, учитесь, и все будет получаться. Читайте нормальную литературу, вроде

читаешь в массив и пишешь в др. файл в обратном порядке.
Похожие вопросы
- Нужно получить массив слов из текстового файла
- задача TurboPascal с текстовым файлом, помогите....
- Написать программу которая подсчитывает частоту встречаемости символов в заданном текстовом файле С++
- Извлечение ссылки из текстовых файлов
- Скопировать из текстового файла .txt часть строк c 1 по 3000, затем с 3000 по 6000 и так до конца файла.
- Как подсчитать дубли строк в текстовом файле?
- Считывание данных из текстового файла С++
- Создать список из повторяющихся слов текста из файла. Первый элемент-наиб. часто повторяющееся слово/ Паскаль. (+)
- Скажите пожалуйста как передать данные из текстового файла в массив на php
- 4. Даны два текстовых файла. Записать в третий только те строки, которые есть и в первом, и во втором файлах.