-
Notifications
You must be signed in to change notification settings - Fork 7
/
seekp&seekg.cpp
58 lines (51 loc) · 2.13 KB
/
seekp&seekg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<fstream>
using namespace std;
int main() {
/*
seekg() (get pointer)
--> Defined in istream class
--> Prototype:
- istream& seekg(streampos pos);
- isteeam& seekg(streamoff off, ios_base::seekdir way);
--> pos is the new absolute position within the stream (relative to the beginning)
--> off is the offset value, relative to the way parameter
--> way values (one of the 3) --> ios_base::beg, ios_base::cur and ios_base::end
seekp() (put pointer)
--> Defined in ostream class
--> Prototype:
- ostream& seekg(streampos pos);
- ostream& seekg(streamoff off, ios_base::seekdir way);
--> pos is the new absolute position within the stream (relative to the beginning)
--> off is the offset value, relative to the way parameter
--> way values (one of the 3) --> ios_base::beg, ios_base::cur and ios_base::end
*/
cout << "For seekg()..\n\n";
ifstream fin;
fin.open("fileHandling.txt");
if (fin.tellg() == -1) {
cout << "Could not open the file\n";
exit(0);
}
cout << "File Opened Successfully\n";
cout << "Tellg position: " << fin.tellg() << endl;
cout << "First char from the file: " << (char)fin.get() << endl;
cout << "Second char from the file: " << (char)fin.get() << endl;
cout << "Tellg position: " << fin.tellg() << endl;
cout << "Making tellg again point to start of the file..\n";
fin.seekg(2);
cout << "Tellg position: " << fin.tellg() << endl;
cout << "Third char from the file: " << (char)fin.get() << endl;
cout << "Making tellg move 7 positions ahead from current..\n";
fin.seekg(7, ios_base::cur);
cout << "Tellg position: " << fin.tellg() << endl;
cout << "Char from the file: " << (char)fin.get() << endl;
fin.close();
cout << "For seekp()\n\n";
ofstream fout;
fout.open("fileHandling.txt", ios::ate | ios::app);
cout << "Tellp position: " << fout.tellp() << endl;
cout << "Making tellp point to 4 position to the right of beginning..\n";
fout.seekp(4, ios_base::beg);
cout << "Tellp position: " << fout.tellp() << endl;
}