Originally Posted by
CommonTater
See... now that was too easy...
Code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{ ifstream inFile;
vector<string> myList;
string x;
inFile.open("test.txt");
while (inFile >> x)
{ myList.push_back(x); }
inFile.close();
sort(myList.begin(),myList.end());
ofstream outFile;
outFile.open("words.txt");
for (int i = 0; i != myList.size(); i++)
{ outFile << *i << endl;
cout << myList[i] << endl; }
outFile.close();
return 0; }
>> outFile << *i << endl;
Except...'i' is an int.
Here's yet another way to do it, by the way:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
int main()
{
using namespace std;
ifstream inFile("test.txt");
vector<string> myList;
copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(myList));
sort(myList.begin(), myList.end());
ofstream outFile("words.txt");
copy(myList.begin(), myList.end(), ostream_iterator<string>(outFile, "\n"));
return 0;
}