Files and Streams (Part III) : Imran Siddiqi Dept. of CS Bahria University, Islamabad Imran - Siddiqi@bahria - Edu.pk
Files and Streams (Part III) : Imran Siddiqi Dept. of CS Bahria University, Islamabad Imran - Siddiqi@bahria - Edu.pk
Files and Streams (Part III) : Imran Siddiqi Dept. of CS Bahria University, Islamabad Imran - Siddiqi@bahria - Edu.pk
(Part III)
Imran Siddiqi
Dept. of CS
Bahria University, Islamabad
[email protected]
Recorder Version @:
https://web.microsoftstream.com/video/dd4c3510-99a1-4dd5-b272-db088515a158
Recap
• ifstream and ofstream objects for reading and
writing
• << and >> operators – Sequence of characters
• read() and write() methods – Binary Mode
• seek() and tell() methods to manipulate file
pointer
//Display data of all students with GPA > 2.5
int main() {
Student s;
ifstream f;
f.open("std.dat", ios::binary);
f.seekg(0, ios::end);
int endPos = f.tellg();
f.seekg(0);
int numberOfRecords = endPos / sizeof(Student);
f.close();
}
//Search for record of student :150
int searchID;
cout << "Enter ID of student to search";
cin >> searchID;
break;
}
}
Disk I/O using Member functions
class Student {
private:
int ID;
float gpa; string address;
public:
void print() {
cout << "ID:" << ID << " GPA:" << gpa << endl;
}
int getID() { return ID; }
float getGPA() { return gpa; }
void setID(int i) { ID = i; }
void setGPA(float g) { gpa = g; }
void input() {
cout << "Enter ID:"; cin >> ID;
cout << "Enter GPA:"; cin >> gpa;
}
};
int Student::countRecords() {
void Student::writeToDisk() {
}
int main() {
int c=Student::countRecords();
cout << "Number of Records:" << c << endl;
Student s2;
s2.readFromDisk(1);
s2.print();
return 0;
}
int main() {
Student s;
char ch;
do { //save students to disk
int n = Student::countRecords();
cout << "There are " << n << " student in file\n";
private:
int x;
int y;
public:
Point(int a = 0, int b = 0) {
x = a;
y = b;
}
};
ostream& operator<<(ostream &s, Point p) {
s << "X:" << p.x << " Y:" << p.y << endl;
return s;
}
Point p;
cin >> p;
cout << p << endl;
return 0;
}