15th Week program Reading-binary and binary files read 3

Source: Internet
Author: User

3, read the following procedure, point out its function, realize the function of SEEKG (), TELLG () function and its usage

(1)

#include <iostream> #include <fstream>using namespace std;const char * filename = "a.txt"; int main () {    Long l,m;    Ifstream file (filename, ios::in|ios::binary);    L = File.tellg ();    FILE.SEEKG (0, ios::end);    m = File.tellg ();    File.close ();    cout << "size of" << filename;    cout << "is" << (m-l) << "bytes.\n";    return 0;}

Operation Result:


(2)

#include <fstream>using namespace Std;int main () {    long pos;    Ofstream outfile;    Outfile.open ("test.txt");    Outfile.write ("This was an apple");    POS=OUTFILE.TELLP ();    OUTFILE.SEEKP (pos-7);    Outfile.write ("Sam", 4);    Outfile.close ();    return 0;}
Operation Result:



(3)

#include <iostream> #include <fstream> using namespace Std;int main () {         fstream outfile,infile;         Outfile.open ("Data.txt", ios::out);         for (int i=0;i<26;i++)       outfile<< (char) (' A ' +i);         Outfile.close ();         Infile.open ("Data.txt", ios::in);         char ch;         INFILE.SEEKG (6,ios::beg);         if (Infile.get (CH))           cout<<ch;         INFILE.SEEKG (8,ios::beg);         if (Infile.get (CH))                 cout<<ch;         INFILE.SEEKG ( -8,ios::end);    if (Infile.get (CH))                 cout<<ch;             cout<<endl;         Infile.close ();     return 0; }

Operation Result:





15th Week program Reading-binary and binary files read 3

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.