assignment_4[1]
assignment_4[1]
assignment_4[1]
Submitted by:
Section: BCS-2ND B.
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.
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>
int main() {
if (outFile.is_open()) {
} else {
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>
int main() {
if (outFile.is_open()) {
outFile.write(reinterpret_cast<char*>(&number), sizeof(number)); //
Write to the file
} else {
return 0;
}
Question 2: what are the four file handling functions?
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?
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.