0% found this document useful (0 votes)
70 views11 pages

Files and Streams (Part III) : Imran Siddiqi Dept. of CS Bahria University, Islamabad Imran - Siddiqi@bahria - Edu.pk

This document discusses file input and output (I/O) in C++ using ifstream, ofstream, and member functions. It shows how to [1] read and write binary data from files using read() and write() methods, [2] manipulate the file pointer using seek() and tell(), [3] overload insertion and extraction operators to read from and write to files, and [4] implement member functions to read and write objects directly to files. Examples are provided to count records in a file, search for a record by ID, and load/store student data to disk.

Uploaded by

Muhammad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views11 pages

Files and Streams (Part III) : Imran Siddiqi Dept. of CS Bahria University, Islamabad Imran - Siddiqi@bahria - Edu.pk

This document discusses file input and output (I/O) in C++ using ifstream, ofstream, and member functions. It shows how to [1] read and write binary data from files using read() and write() methods, [2] manipulate the file pointer using seek() and tell(), [3] overload insertion and extraction operators to read from and write to files, and [4] implement member functions to read and write objects directly to files. Examples are provided to count records in a file, search for a record by ID, and load/store student data to disk.

Uploaded by

Muhammad Haider
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Files and Streams

(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);

for (int i = 0; i < numberOfRecords; i++) {


f.read(reinterpret_cast<char *>(&s), sizeof(s));
if(s.getGPA()>2.5)
s.print();
}

f.close();
}
//Search for record of student :150
int searchID;
cout << "Enter ID of student to search";
cin >> searchID;

for (int i = 0; i < numberOfRecords; i++) {


f.read(reinterpret_cast<char *>(&s), sizeof(s));
if (s.getID() == searchID) {
//Enter new address
s.SetAddress(new address)

break;
}
}
Disk I/O using Member functions
class Student {
private:
int ID;
float gpa; string address;
public:

Student(int i = 0, float g = 0.0) {


ID = i;
gpa = g;
}

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;
}

void readFromDisk(int number);


void writeToDisk();
static int countRecords();

};

int Student::countRecords() {

ifstream f("Students.dat", ios::binary);


f.seekg(0, ios::end);
int loc = f.tellg();

return loc / sizeof(Student);


}
void Student::readFromDisk(int number) {

ifstream f("Students.dat", ios::binary);


int loc = (number - 1) * sizeof(Student);
f.seekg(loc);
f.read((char *)(this), sizeof(Student));
f.close();

void Student::writeToDisk() {

ofstream f("Students.dat", ios::app | ios::binary);


f.write((char *)(this), sizeof(Student));
f.close();

}
int main() {

Student s1(151, 3.5);


s1.writeToDisk();

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

cout << "Enter data for student:";


s.input(); //get data
s.writeToDisk(); //write to disk
cout << "Do another(y / n) ? ";
cin >> ch;
} while (ch == ’y’); //until user enters ‘n’

int n = Student::countRecords();
cout << "There are " << n << " student in file\n";

for (int j = 1; j<=n; j++) //for each one,


{
cout << "\nStudent: " << j;
s.readFromDisk(j); //read student from disk
s.print(); //display data
}
return 0;
}
Overloading Extraction and Insertion
Operators
class Point {

private:
int x;
int y;

public:
Point(int a = 0, int b = 0) {
x = a;
y = b;
}

friend ostream& operator<<(ostream &, Point);


friend istream& operator >> (istream &, Point&);

};
ostream& operator<<(ostream &s, Point p) {

s << "X:" << p.x << " Y:" << p.y << endl;
return s;
}

istream& operator>>(istream &s, Point &p) {

cout << "Enter x:"; cin >> p.x;


cout << "Enter y:"; cin >> p.y;
return s;
}
int main() {

Point p;
cin >> p;
cout << p << endl;

return 0;
}

You might also like