Write a program that reads a list of
names from a file called "names.txt" until it reaches end
of file, sorts them, and writes them to a second file. Be
sure not to exceed the array bounds.
1
Expert's answer
2015-10-22T02:30:54-0400
#include <string> #include <iostream> #include <fstream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list<string> lines; int n=0; string a; ifstream F; F.open("D:\\data.txt", ios::in); if (F){ while (!F.eof()){ F>>a; lines.push_back(a); } F.close(); } else cout<<"Error"<<endl; lines.sort(); ofstream out("output.txt"); copy(lines.begin(), lines.end(), ostream_iterator<string>(out, "\n")); out.close(); return 0; }
Comments
Leave a comment