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