Reads each line of text ("\ n"), stores the array vector<string>, and outputs the last line less.
Code:
* * * main.cpp * *
Created on:2014.06.08 * author:spike * *
/*vs 2012*/
# Include <windows.h>
#include <fstream>
#include <iostream>
#include <string >
#include <vector>
using namespace std;
int main ()
{
vector<string> tmp_files;
Ifstream infile ("W.txt");
if (!infile) {
cout << "fail!" << Endl;
return 0;
}
string linecontent;
while (Getline (infile, linecontent, ' \ n ')) {
Tmp_files.push_back (linecontent + "\ n");
}
Infile.close ();
Ofstream outfile ("W2.txt", ios::out);
Vector<string>::iterator siter = Tmp_files.begin ();
Copy (Tmp_files.begin (), Tmp_files.end ()-1, ostream_iterator<string> (outfile));
cout << "ok!" << Endl;
Outfile.close ();
return 0;
}
Output:
Original text (w.txt):
Female
Sister
Girl
Woman old
Woman
output text (w2.txt):
Female
Sister
Girl
Woman
Author: csdn Blog spike_king
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/