-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession17.cpp
More file actions
43 lines (28 loc) · 821 Bytes
/
Session17.cpp
File metadata and controls
43 lines (28 loc) · 821 Bytes
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
#include<iostream>
#include<fstream>
using namespace std;
// Random Access of Data in Files
int main(){
fstream stream;
stream.open("MyStudents.txt", ios::in | ios::out | ios::app);
// GetPointer
// PutPointer
// Read Positions of Pointers -> tellg and tellp functions
stream.seekg(5, ios::beg);
int tgPos = stream.tellg();
int tpPos = stream.tellp();
cout<<"GetPointer pos is:"<<tgPos<<"\n";
cout<<"PutPointer pos is:"<<tpPos<<"\n";
char data[5];
stream.read(data, 4);
cout<<"data is:"<<data<<"\n";
stream.seekg(0, ios::end);
int size = stream.tellg();
cout<<"Size is: "<<size<<"\n";
stream.seekg(0, ios::beg);
char buffer[size+1];
stream.read(buffer, size);
buffer[size+1] = 0; // null character at the end is termination of string
cout<<"buffer is:"<<buffer<<"\n";
return 0;
}