assignment_4[1]

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Programming Fundamental:

Submitted by:

Ahmed Hassan (064)

Section: BCS-2ND B.

Assignment 4. Deadline: Thursday

Question 1: What is file handling and it’s types in C++ with examples?

File Handling: File handllng deals with the process of creating text fiel and
storing them on the desired location.

File handling in C++ refers to the process of working with files, such as
reading from or writing to them. There are mainly two types of file handling
in C++: text file handling and binary file handling.

Types of file handling:

Test File Handling: Deals with handling of text files. Text file handling
involves reading from and writing to files in a human-readable format.

Example:

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ofstream outFile("example.txt"); // Create and open a text file for writing

if (outFile.is_open()) {

outFile << "Hello, World!\n"; // Write to the file

outFile << "This is a test.\n";


outFile.close(); // Close the file

} else {

cout << "Unable to open file";

return 0;

Binary Handling: Deals with handling bunary data. Binary file handling
deals with reading from and writing to files in their raw binary format, which
is not human-readable.

Example :

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ofstream outFile("example.bin", ios::binary); // Open a binary file for


writing

int number = 12345;

if (outFile.is_open()) {

outFile.write(reinterpret_cast<char*>(&number), sizeof(number)); //
Write to the file

outFile.close(); // Close the file

} else {

cout << "Unable to open file";

return 0;

}
Question 2: what are the four file handling functions?

The four primarily main function of file handling are as following:

1. Opening.
2. Wrinting.
3. Reading.
4. Closing.

he four main file handling functions in C++ are open, close, write, and read. Here's a brief
explanation of each:

1. open:
o The open function is used to open a file before performing any operations on it.
o It takes the file name as a parameter along with optional flags that specify the
mode of file access (e.g., read, write, append, binary).
o Example: std::ofstream outfile("example.txt");
2. close:
o The close function is used to close a file after all operations have been
completed.
o It is important to close files properly to release system resources and ensure data
integrity.
o Example: outfile.close();
3. write:
o The write function is used to write data to a file.
o It takes the data to be written and the number of bytes to write as parameters.
o Example: outfile.write(reinterpret_cast<char*>(&data),
sizeof(data));
4. read:
o The read function is used to read data from a file.
o It takes the buffer where the data will be stored and the number of bytes to read as
parameters.
o Example: infile.read(reinterpret_cast<char*>(&data),
sizeof(data));
Question 3: why is file handling used?

Flie handling is crucial in saving, managing and recording large data.


 Data Persistence:

 Files provide a way to store data persistently on disk. This means that the data remains
intact even after the program that created it has terminated. File handling allows
programs to read and write data to files, making it useful for storing information between
sessions or sharing data with other programs.

 Data Sharing:

 Files are a common medium for sharing data between different programs or systems. For
example, a program can write data to a file, and another program can read that data later.
This is especially useful for communication between different software components or
across networked systems.

 Data Backup:

 Files can serve as a backup mechanism for important data. By writing data to files
regularly, programs can create backup copies that can be used to restore data in case of
system failures or data corruption.

 Data Analysis:

 File handling enables programs to read large amounts of data from files for analysis or
processing. For example, a program can read a text file containing sales data and perform
calculations to generate reports or insights.

Question 4: define test harnesses in programming?

Test harness in programming is a collection of software and tools used for


monitoring the behaviour and output of components under certain condition.

You might also like