Lab 12
Lab 12
Laboratory 12
File Processing
Version: 1.0.0
Contents:
Learning Objectives
Required Resources
General Instructions
Background and Overview
o Files and Streams
o Sequential File
o Random-Access File
Activities
o Pre-Lab Activity
Files and Streams
Creating a Sequential File
– Opening a file
– Closing a file
Task 01
Task 02
o In-Lab Activity
File Position Pointers
– Member functions tellp() and tellg()
Updating a Sequential File
Creating a Random File
Writing Data to a Random File
Reading Data from a Random File
Task 01
Task 02
Task 03
o Post-Lab Activity
Task 01
Submissions
Evaluations Metric
References and Additional Material
Lab Time and Activity Simulation Log
Learning Objectives:
Files and Streams
Create a Sequential File
Read a Sequential File
Update a Sequential File
Random Access File
Create a Random-Access File
Read a Random-Access File
Resources Required:
Desktop Computer or Laptop
Microsoft ® Visual Studio 2022
General Instructions:
In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY discuss
with your Teaching Assistants (TAs) or Lab Instructor.
Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Teachers:
Course Instructor Prof. Dr. Syed Waqar ul Qounain [email protected]
Lab Instructor Azka Saddiqa [email protected]
A stream is an abstraction that represents a device on which operations of input and output are
performed. A stream can be represented as a source or destination of characters of indefinite length
depending on its usage.
Sequential File:
A sequential file is simply a file where the data is stored one item after another. A more technical
definition would be that a sequential file is a collection of data stored on a disk in a sequential, non-
indexed, manner. C++ treats all such files simply as a sequence of bytes. Each file ends with a special
end of file marker.
Random-Access File:
Random file access enables us to read or write any data in our disk file without having to read or write
every piece of data before it while in Sequential files to reach data in the middle of the file you must go
through all the data that precedes it. We can quickly search for data, modify data, delete data in a
random-access file.
Activities:
Pre-Lab Activities:
Files and Streams:
C++ views each file simply as a sequence of bytes. Each file ends either with an end-of-file marker or
at a specific byte number recorded in an operating-system-maintained administrative data structure.
When a file is opened, an object is created, and a stream is associated with the object.
To perform file processing in C++, headers <iostream> and <fstream> must be included.
Header <fstream> includes the definitions for the stream class templates
Ofstream outClientFile;
outClientFile.open(Filename, Mode)
Following are the different modes to open a file:
Mode Description
ios::app Append all output to the end of the file.
ios::ate Open a file for output and move to the end of the file. Data can be written
anywhere in the file.
ios::in Open file for input.
ios::out Open file for output.
ios::trunc Discard’s the file content.
ios::binary Open a file for binary.
Closing a file:
File is closed implicitly when a destructor for the corresponding object is called. It can also be called
explicitly by using member function close:
outClientFile.close();
Reading a Sequential file:
Example:
File Content:
Output:
In-Lab Activities:
File Position Pointers:
<istream> and <ostream> classes provide member functions for repositioning the file pointer (the byte
number of the next byte in the file to be read or to be written). These member functions are:
seekg (seek get) for istream class
seekp (seek put) for ostream class
The same operations can be performed with <ostream> function member seekp.
Example:
In this example, we open a file named “example.txt” for writing using std::ofstream. We then write
some data to the file using the << operator. We use the tellp() function to get the current position of the
file pointer and use seekp() to move the file pointer back to the beginning of the file. We then write
some more data to the file and use seekp() again to move the file pointer to the end of the file. Finally,
we write some more data to the file and close it.
File Content:
Example:
In this example, we open a file named "example.txt" and use the tellg() function to get the current
position of the file pointer. We then use the seekg() function to move the file pointer to the end of the
file and use tellg() again to get the size of the file. We then move the file pointer back to the beginning
of the file using seekg() and read the data from the file using the read() function. Finally, we output the
data to the console and close the file.
Output:
Example:
Following example demonstrates the overwriting of data while trying to update a sequential file.
Output:
Example:
The <ostream> member function write outputs a fixed number of bytes beginning at a specific location
in memory to the specific stream. When the stream is associated with a file, the data is written beginning
at the location in the file specified by the “put” file pointer. The write function expects a first argument
of type “const char *”, hence we used the reinterpret_cast to convert the address of the blankClient to
a const char *. The second argument of write is an integer of type “size_t” specifying the number of
bytes to written. Thus, the sizeof (clientData).
Output:
Output:
Create a class named Student to store information about a student, including their name, ID, GPA,
and list of courses.
Implement the following methods in the class:
o void setStudentData(): This method should prompt the user to enter the student's name, ID,
GPA, and list of courses, and store the data in the object's member variables.
o void printStudentData(): This method should print the student's name, ID, GPA, and list of
courses to the console.
o void writeToFile(std::ofstream& file): This method should write the student's data to the
specified file in a custom format. Each line in the file should contain the student's name, ID,
GPA, and comma-separated list of courses.
o void readFromFile(std::ifstream& file): This method should read the student's data from
the specified file in the custom format and store it in the object's member variables.
In your main() function, create an array of Student objects, prompt the user to enter data for each
student, and write the data to a file named "studentRecord.txt". Then, read the data from the file
and print it to the console.
Post-Lab Activities:
Task 01: Library Management [Estimated time 60 minutes / 40 marks]
Create a program that simulates a library management system. Your program should have the
following classes:
Book class: This class should contain information about a book, including its title, author,
publisher, publication year, ISBN, and status (e.g., available, checked out). Implement the
following methods in the class:
o void setBookData(): This method should prompt the user to enter the book's data, and store
the data in the object's member variables.
o void printBookData(): This method should print the book's data to the console.
o void writeToFile(std::ofstream& file): This method should write the book's data to the
specified file.
o void readFromFile(std::ifstream& file): This method should read the book's data from the
specified file and store it in the object's member variables.
Library class: This class should contain information about a library, including its name, address,
and list of books. Implement the following methods in the class:
o void addBook(Book b): This method should add the specified book to the library's list of
books.
o void removeBook(Book b): This method should remove the specified book from the library's
list of books.
o void printLibraryData(): This method should print the library's name, address, and list of
books to the console.
o void writeToFile(std::ofstream& file): This method should write the library's data to the
specified file in a custom format. The first line of the file should contain the library's name
and address, followed by a blank line. Each subsequent line should contain the data for one
book.
o void readFromFile(std::ifstream& file): This method should read the library's data from the
specified file in the custom format and store it in the object's member variables.
In your main() function, create a Library object, prompt the user to enter data for the library and
its books, and write the data to a file named "library.txt". Then, read the data from the file and
print it to the console.
Your program should also allow the user to check out and return books from the library.
Implement the following methods for the Library class:
o void checkOutBook(Book b): This method should mark the specified book as checked out.
o void returnBook(Book b): This method should mark the specified book as available again.
Submissions:
For In-Lab Activity:
Save the files on your PC.
TA’s will evaluate the tasks offline.
For Pre-Lab & Post-Lab Activity:
Submit the .cpp file on Google Classroom and name it to your roll no.
Evaluations Metric:
All the lab tasks will be evaluated offline by TA’s
Division of Pre -Lab marks: [30 marks]
Task 01: Word Occurrence [15 marks]
Task 02: Map 2D Array [15 marks]
Division of In-Lab marks: [70 marks]
Task 01: Compare Files [20 marks]
Task 02: Students Rubric [20 marks]
Task 03: Student Record [30 marks]
Division of Post-Lab marks: [40 marks]
Task 01: Library Management [40 marks]