C + + file offset

Source: Internet
Author: User

1#include <iostream>2#include <fstream>3#include <cassert>4 5 using namespacestd;6 intMain ()7 {8Ifstreaminch("Test_data.txt");9Assertinch. Is_open ());Ten  One     //The base address is at the end of the file with an offset of 0, so the pointer is positioned at the end of the file A     inch. SEEKG (0, ios::end); -     //The SP is a positional pointer because it is at the end of the file, so that is the size of the file -Streampos SP =inch. TELLG ();  thecout<<"File Size:"<<sp<<Endl; -  -     //base Address is the end of the file, the offset address is negative, so move forward Sp/3 bytes -     inch. SEEKG (-sp/3, Ios::end);  +Streampos SP2 =inch. TELLG (); -cout<<"From file to point :"<<sp2<<Endl; +  A     //base Address is the file header, the offset is 0, so locate in the file header; Read the contents of the file from the beginning at     inch. SEEKG (0, Ios::beg); -cout<<inch. RDBUF () <<endl;//All the contents of all output files due to the offset from the beginning -  -     //read the contents of the file from SP2, i.e. everything behind SP2 -     inch. SEEKG (SP2); -cout<<inch. RDBUF () <<Endl; in  -     inch. Close (); to  +     return 0; -}

C + + file offset

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.