You are on page 1of 7

Object Oriented Programming

Lab Manual

Topic: Filing

Lab Instructor:
Ahmad Abduhu

Session: Spring 2020

School of Systems and Technology


UMT Lahore Pakistan

1
Sample Code 1:

//A simple program to write a string into a file.

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  // Create and open a text file
  ofstream MyFile("filename.txt");

  // Write to the file


  MyFile << "Files can be tricky, but it is fun enough!";

  // Close the file


  MyFile.close();
}

Sample Code 2:

//A simple program to write a string into a file and then reading back from the same file .

#include <iostream>
#include <fstream>
using namespace std;

int main () {
// Create a text file
ofstream MyWriteFile("filename.txt");

// Write to the file


MyWriteFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyWriteFile.close();

// Create a text string, which is used to output the text file


string myText;

// Read from the text file


ifstream MyReadFile("filename.txt");

getline (MyReadFile, myText);

cout << myText<<endl;


}

Sample Code 3:

2
//This program open a file to read its contents line by line.

#include <iostream>
#include <fstream>
using namespace std;

int main () {

string myText;

ifstream MyReadFile;

MyReadFile.open("file.txt", ios::in);

while( getline(MyReadFile,myText))
cout << myText<<endl;

MyReadFile.close();

Sample Code 4:

//This program write complete record to a file and read the same record from the file and show it on the
screen.

#include <iostream>
#include <fstream>
using namespace std;

struct student{
int id;
string name;
double cgpa;
};
int main () {

student st;
st.id = 123;
st.name = "Ali Ahmad";
st.cgpa = 3.57;

ofstream WriteFile;
WriteFile.open("newFile.txt", ios::out);

WriteFile.write(reinterpret_cast<char*>(&st),sizeof(student));

3
WriteFile.close();

ifstream ReadFile;

ReadFile.open("newFile.txt");

student st1;

ReadFile.read(reinterpret_cast<char*>(&st1),sizeof(student));

cout<<st1.id<<" "<<st1.name<<" "<<st1.cgpa<<endl;

Sample Code 5:

#include <iostream>
#include <iomanip>
#include <string>
#include<fstream>

using namespace std;

class Student
{
private:
int ID;
string firstname;
string lastname;
char Grade;
int score;

public:
Student()
{

}
Student(int id, string fn,string ln, char m, int s)
{
setID(id);
setfn(fn);
setln(ln);
setgrade(m);
setscore(s);
}
void setID(int id) { ID = id; }
void setfn(string f) { firstname = f; }

4
void setln(string l) { lastname = l; } //setters
void setgrade(char g) { Grade = g; }
void setscore(int s) { score = s; }

int getID() { return ID; }


string getfn() { return firstname; }
string getln() { return lastname; } //getters
char getgrade() { return Grade; }
int getscore() { return score; }
};

int main()
{
Student Ahmad(1,"Ahmad","Abduhu",'B',85);
Student Hasnain(2,"Hasnain","Haider",'A',90);
Student Ali(5,"Ali","Khan",'B',72);
Student Izhar(3,"Izhar","Ahmad",'F',43);
Student Farooq(4,"Syed","Farooq",'A',88);
Student Zahid(6,"Zahid","Hussain",'A',70);
Student AliH(7,"Ali","Haider",'B',68);
Student Muhammad(8,"Muhammad","Ahmad",'A',65);

Student reader;
fstream obj;

cout<<" Writting Records To a File"<<endl;

obj.open("records.txt",ios::out);
obj.write(reinterpret_cast<char*>(&Ahmad),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Hasnain),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Izhar),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Farooq),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Ali),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Zahid),sizeof(Student));
obj.write(reinterpret_cast<char*>(&Muhammad),sizeof(Student));
obj.write(reinterpret_cast<char*>(&AliH),sizeof(Student));

obj.close();

fstream inFile("records.txt", ios::in );

cout<<" Reading Records.. \n\n";

inFile.read(reinterpret_cast<char*>(&reader),sizeof(Student));

while( !inFile.eof())
{
cout<< setw(2) <<reader.getID()

5
<< setw(10) <<reader.getfn()
<< setw(10) <<reader.getln()
<< setw(3) <<reader.getgrade()
<< setw(5) <<reader.getscore()
<< endl;
inFile.read(reinterpret_cast<char*>(&reader),sizeof(Student));
}

return 0;
}

6
Lab Tasks
Task 01:
Modify the 5th Sample Task by adding 5 more students and do the following:
1. Print all records.
2. Print only ID, First name and Grade of those student(s) who secured 70+ marks
3. Print the complete records of student(s) who’s first or last name is “Ahmad”
4. Print the record of students with even IDs.
5. Print just Name and ID of student(s) with ‘F’ grade.

Note: Create a menu of above five options using switch statement and allow the user to select one of
them. (You may add some other possibilities as well).

Task 02:
Modify the 5th Sample Task:
1. Create an Array of Student class with the size of at least 20 elements, Like:
a. Student Records[20];
2. Modify your student class which generate automatic IDs for each student.
a. For that purpose you should use a static data member IDGenerator which assign IDs to
each student.
3. Initialize with proper values.
4. Write all 20 records to a file “FinalResult.txt” using for loop.
5. Read back all records in an array again using for loop like:
a. Students ReadRecords[20];
6. Must add some records with ‘F’ grade.

You might also like