0% found this document useful (0 votes)
12 views9 pages

OOP1

Lab #01 focuses on introducing file handling in C++ and includes pre-lab reading and writing assignments to prepare students for practical tasks. Students will learn to perform file operations such as reading, writing, and appending data using various classes and functions from the standard I/O library. The lab tasks include programming exercises that reinforce the concepts of file handling and basic C++ syntax.

Uploaded by

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

OOP1

Lab #01 focuses on introducing file handling in C++ and includes pre-lab reading and writing assignments to prepare students for practical tasks. Students will learn to perform file operations such as reading, writing, and appending data using various classes and functions from the standard I/O library. The lab tasks include programming exercises that reinforce the concepts of file handling and basic C++ syntax.

Uploaded by

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

Lab # 01

Object Oriented Programming


Fall 2024

Instructor Bakht Muhammad


Student Name UZAIR KAKAR
CMSID 513802
Department Artificial Intelligence
Section A

Lesson Set Introduction to File handling


1 And recall basics of C++
Purpose • To get basic awareness of file handling
• To understand file handling and why we are using it.
• To perform various example

Procedure • Students should read the Pre-lab Reading assignment before coming to lab.
• Students should complete the Pre-lab Writing assignment before coming to lab.
• In the lab, students should complete Labs1.1 through 1.2 in sequence. Your
instructor will give further instructions as to grading and completion of the
lab.
• Students should complete the set of lab tasks before the next lab and get
them checked by their lab instructor.

Contents Pre-requisites Completion Page


Time Number
Pre-lab Reading Assignment - 20 min 3
Pre-lab Writing Assignment Pre-lab Reading 10 min 4
Lab 1
Lab 1.1 Pre-lab reading 30 min 5
File handling
Lab 1.2 C++ basics - 9
Lab Tasks

PRE-LAB READING ASSIGNMENT


What is File Handling In C++, file handling is typically done using the standard I/O library,
which includes several classes and functions for working with files.
The iostream header provides the ifstream, ofstream, and fstream
classes for reading from, writing to, and reading from/writing to files,
respectively.

The basic steps for file handling in C++ are:

• Open the file using an appropriate stream class (ifstream,


ofstream, or fstream) and the open() function.
• Perform the desired input/output operations using the stream's
input/output operators (e.g., << and >> for formatted I/O).
• Close the file using the close() function.

File handling in C++ allows you to work with a wide variety of files,
including text files, binary files, and other types of files. You can read
data from files, write data to files, and manipulate the contents of files
in various ways. This can be useful for a variety of applications, such
as data processing, file conversion, and more.

File handling in C++ involves performing various operations on files


such as reading data from a file, writing data to a file, opening and
closing a file, positioning the file pointer, etc. Some of the functions
used for file handling in C++ are:
• fstream: This is a class used for reading and writing data to a
file. It provides functions for opening and closing files, writing
data to a file, reading data from a file, positioning the file
pointer, etc.
• ifstream: This is a class used for reading data from a file. It
provides functions for opening and closing files, reading data
from a file, positioning the file pointer, etc.
• ofstream: This is a class used for writing data to a file. It
provides functions for opening and closing files, writing data to
a file, positioning the file pointer, etc.
• open(): This function is used to open a file. It takes the
filename as input and opens the file in the specified mode
(read, write, append, etc.).
• close(): This function is used to close a file that has been
opened using the open() function.
• getline(): This function is used to read a line of text from a
file.
• put(): This function is used to write a character to a file.
• get(): This function is used to read a character from a file.
• tellg(): This function is used to get the current position of the
file pointer in a file that is being read.
• seekg(): This function is used to set the position of the file
pointer in a file that is being read.
• tellp(): This function is used to get the current position of the
file pointer in a file that is being written.
• seekp(): This function is used to set the position of the file
pointer in a file that is being written.
• These are some of the functions used for file handling in C++.
By using these functions, you can perform various operations
on files such as reading data from a file, writing data to a file,
opening and closing a file, positioning the file pointer, etc.

In C++, the open() function can be used to open a file in different


modes. Here are the different open modes that can be used in C++:

• ios::in: This mode is used for reading data from a file.


• ios::out: This mode is used for writing data to a file. If the file
already exists, the existing contents of the file will be
overwritten.
• ios::app: This mode is used for appending data to a file. If the
file already exists, any new data written to the file will be added
to the end of the file.
• ios::ate: This mode is used to set the file pointer to the end of
the file when the file is opened. This is useful when you want to
read or write data to the end of the file.
• ios::trunc: This mode is used to truncate the file to zero length
when it is opened. This means that any existing data in the file
will be deleted.
• ios::binary: This mode is used to open a file in binary mode.
This is useful when you want to read or write binary data to a
file.

Opening a file and #include <fstream>


writing to it: using namespace std;
int main() {
ofstream outfile;
outfile.open("example.txt");

outfile << "This is some text.\n";


outfile << "This is some more text.\n";
outfile.close();
return 0;
}

In this example, we first include the fstream header and create an


ofstream object called outfile. We then use the open function to open a
file called example.txt. We can then use the << operator to write text to
the file, just like we would use cout to write to the console. Finally, we
close the file using the close function.
Reading from a file: #include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream infile;
infile.open("example.txt");
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
return 0;
}
In this example, we create an ifstream object called infile and use the
open function to open a file called example.txt. We then use a while
loop and the getline function to read each line of the file into a string
variable called line. Finally, we output each line to the console using
cout.
Appending to a file: #include <fstream>
using namespace std;
int main() {
ofstream outfile;
outfile.open("example.txt", ios::app);
outfile << "This is some additional text.\n";
outfile.close();
return 0;
}
In this example, we create an ofstream object called outfile and use
the open function with the ios::app flag to open the file in append
mode. We can then use the << operator to append text to the file.
Checking if a file #include <fstream>
exists: using namespace std;
bool file_exists(const string& filename) {
ifstream infile(filename);
return infile.good();
}
int main() {
if (file_exists("example.txt")) {
cout << "The file exists.\n";
} else {
cout << "The file does not exist.\n";
}
return 0;
}
In this example, we define a function called file_exists that takes a
filename as a parameter and returns a bool indicating whether the file
exists or not. We do this by creating an ifstream object with the given
filename and using the good function to check if the file is open and
readable. We can then use this function to check if a file exists in our
main function.

PRELAB WRITING ASSIGNMENT


Fill in the blanks • In C++, file handling involves opening and closing a file using
the appropriate file __fstream_____.
• The most common modes used for opening a file are
____read___ and ___write____.
• The fstream library provides two classes for file handling:
____ifstream___ and ___ofstream____.
• To read data from a file, you can use the ___>>____ operator or
the getline function.
• When you are finished working with a file, you should always
remember to ____close()___ it to avoid any data loss or
corruption.
Lab Tasks
Lab 1.2
• Write a program to draw diamond pattern using nested loops in C++.

ANSWER:
#include <iostream>
using namespace std;

int main() {
int n;

cout << "Enter the number of rows (half the height of the diamond): ";
cin >> n;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n - i; j++) {


cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}

for (int i = n - 1; i >= 1; i--) {


for (int j = 1; j <= n - i; j++) {
cout << " ";
}

for (int k = 1; k <= 2 * i - 1; k++) {


cout << "*";
}
cout << endl;
}

return 0;
}

OUTPUT:

• Write a program to search word in string list.

ANSWER:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
vector<string> strList = {"ali", "ahmad", "", "abbas”,"nasir"};
string searchWord;

cout << "Enter word to search: ";


cin >> searchWord;

for (const auto& word : strList) {


if (word == searchWord) {
cout << "Found: " << searchWord << endl;
return 0;
}
}

cout << "Not found!" << endl;


return 0;
}

OUTPUT:
• Write a program in C++ to read the numbers (45,56,12,8,5,98) from a file, and
find the largest number in the file, and then write the answer back to the same
file?

ANSWER:

OUTPUT:

• Write a program to access data elements of an array using addresses instead of


indexes.

ANSWER:
#include <iostream>
using namespace std;

int main() {
int arr[] = {10, 20, 30, 40, 50};

int* ptr = arr;

cout << "Element at index 0: " << *ptr << endl;


cout << "Element at index 1: " << *(ptr + 1) << endl;
cout << "Element at index 2: " << *(ptr + 2) << endl;
cout << "Element at index 3: " << *(ptr + 3) << endl;
cout << "Element at index 4: " << *(ptr + 4) << endl;
return 0;
}

OUTPUT:

. Write a program in C++ to read an equation from a file, solve it, and then write the
answer back to the same file?
ANSWER:

You might also like