In C + + random access (random access) stream (stream) detailed

Source: Internet
Author: User
Tags versions

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.