Random access Flow, using mark (Mark) to mark the location of the stream, contains two methods of tell and seek;
Tell, which is the return stream Mark's location, contains G and P two versions. g denotes get, refers to the input stream; P means put, the output stream;
Seek is a jump to the position indicated by the stream mark, and also contains G and P two versions; seek can specify a location or offset (offset);
The code is as follows:
* * * cppprimer.cpp * * Created on:2013.11.28 * author:caroline//*eclipse CDT, gcc 4.8.1*/
#include <iostream> #include <fstream> #include <cstdlib> using namespace std;
int main () {Std::fstream inOut ("Copyout", std::fstream::ate/* end * * | std::fstream::in | std::fstream::out);
if (!inout) {Std::cerr << "unable to open file!" << Std::endl;
return exit_failure;
Std::fstream::p os_type end_mark = Inout.tellg (); INOUT.SEEKG (0, Std::fstream::beg);
Reposition at the beginning of the std::size_t CNT (0);
Std::string Line; while (InOut && inout.tellg ()!= end_mark && getline (InOut, line)) {cnt =
Line.size () + 1; Std::fstream::p os_type mark = Inout.tellg (); Remember the current position INOUT.SEEKP (0, Std::fstream::end);
Skip to end InOut << CNT; if (Mark!= end_mark) inOut << ""; In addition to the last line,are written to space inout.seekg (mark);
Back to the record location} INOUT.SEEKP (0, Std::fstream::end);
InOut << "\ n";
return 0; }
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
Output (text):
Caroline
Wendy
Spike
Winny
9 12 16 21
Author: csdn Blog spike_king