????9?????????????
???????????????????????????????????????????????????λ????????????????????????У????????У??????????????????????????λ??seek??????λ?ò??????tell????????е???λ?á?
????9.1 seek??tell????
????seekg???????λ???????е???
????tellg?????????????б?????λ??
????seekp???????λ??????е???
????tellp????????????б?????λ??
???????????????istream????ifstream????istringstream?????g?汾???????????ostream???????????????ofstream????ostringstream??????p?汾??iostream????fstream????stringstream?????????????????????д??????????????汾??????汾??9.
????9.2 ?????????
?????????????????????????????????汾???????????????????????——??п???????????д????
????????????ifstream?????????tellp???????????????????????????????????
??????ü?????????д??fstream???????stringstream???????????????????????????????????????????е??λ???????????g?汾??p?汾????????????
????9.3 ???iostream??????????????????? 9.4 ?????λ???
????seekg??new_position??;
????seekp (new_position);
????seekg( offset?? dir);
????seekp( offset?? dir);
??????????汾?????λ???л????????????????汾?????????????????δ???????????????
????9.5 ??????
????tell????????????????????????pos_type????????檔
????10????????
????????????????????????????????????βд??????У????а????????п???????λ?????????д????е?????????????????0?????????????????????
????abcd
????efg
????hi
????j
??????γ????????????????????£?
????abcd
????efg
????hi
????j
????5 9 12 14
????#include <iostream>
????#include <fstream>
????#include <string>
????using std::fstream;
????using std::cerr;
????using std::endl;
????using std::ifstream;
????using std::ofstream;
????using std::string;
????using std::getline;
????using std::cout;
????//using namespace std;
????int main()
????{
????fstream inOut("copyOut.txt"??
????fstream::ate | fstream::in | fstream::out);            //??ate??????????????λ???λ??????β??
????if( !inOut )        {
????cerr << "unable to open file" <<endl;
????return EXIT_FAILURE;
????}
????inOut.seekg(-1??fstream::end);            //go to the last char
????if( inOut.peek() != 10)                    //if the last char of the file is not a newline??add it.
????{
????inOut.seekg(0??fstream::end);
????inOut.put(' ');
????}
????inOut.seekg(0??fstream::end);
????ifstream::pos_type endMark = inOut.tellg();        //record the last position .
????inOut.seekg(0??fstream::beg);
????int cnt = 0;                        //accumulator for byte count
????string line;                        //hold each line of input
????while( inOut && inOut.tellg() != endMark
????&& getline(inOut ?? line)
????)
????{
????cnt += line.size() + 1;            // add 1 to acount for the newline
????ifstream::pos_type mark = inOut.tellg();
????inOut.seekp( 0?? fstream::end);    //set write marker to end
????inOut << cnt;
????if( mark != endMark) inOut << " ";
????inOut.seekg(mark);                //restore read position
????}
????inOut.clear();                        //clear flags in case we hit an error
????inOut.seekp(0 ?? fstream::end);        //seek to end
????inOut << endl;                        //write a newline at end of file
????return 0;
????}