Use of Seek and tell--Get File Content size (bytes)

Source: Internet
Author: User

/*gets the size of the data accessed in the file (in bytes) The two member functions of ELLG () and TELLP () do not pass in parameters, returning a value of type Pos_type (according to the ansi-c++ Standard), which is an integer representing the position of the current get stream pointer (with TELLG) or The position of the put stream pointer (with TELLP). SEEKG () and SEEKP () are used to change the position of the flow pointer get and put, respectively. Two functions are overloaded with two different prototypes: SEEKG (Pos_type position); SEEKP (pos_type position); Using this prototype, the flow pointer is changed to point to an absolute position from the beginning of the file. The parameter type required to pass in is the same as the return value type of the function Tellg and TELLP. SEEKG (off_type offset, seekdir direction); SEEKP (off_type offset, seekdir direction); Use this prototype to specify a specific pointer that is determined by the parameter direction Begins the calculation of a displacement (offset). It can be: Ios::beg The displacement calculated from the beginning of the flow ios::cur the displacement calculated from the current position of the flow pointer ios::end the displacement at the end of the stream*/#include<iostream>#include<fstream>using namespacestd;Const Char* Filename="a.txt";intMain () {Longl,m; Ifstream infile (filename,ios::inch|ios::binary);//establishes an input stream in binary form, associating with file A.txt//infile.seekg (0,ios::beg);//Position read pointer location for file startL=infile.tellg ();//gets the current read pointer position (in bytes)INFILE.SEEKG (0, ios::end);//Position read pointer location to end of fileM=infile.tellg ();//gets the current read pointer position (in bytes)Infile.close ();//Close Filecout<<"sizeof"<<filename; cout<<" is"<< (m-l) <<"bytes.\n"; System ("Pause");//pause for a moment    return 0;}

Seek and tell usage--Get File Content size (bytes)

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.