0% found this document useful (0 votes)
18 views7 pages

C++ unit 4 Chap2

This document provides an overview of file-handling, defining it as the management and manipulation of files on a storage device. It covers file-stream classes such as ifstream, ofstream, and fstream, detailing their purposes and functionalities for reading and writing data. Additionally, it discusses standard class functions for file I/O, file opening modes, and the differences between text and binary files.

Uploaded by

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

C++ unit 4 Chap2

This document provides an overview of file-handling, defining it as the management and manipulation of files on a storage device. It covers file-stream classes such as ifstream, ofstream, and fstream, detailing their purposes and functionalities for reading and writing data. Additionally, it discusses standard class functions for file I/O, file opening modes, and the differences between text and binary files.

Uploaded by

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

UNIT 4

FILE-HANDLING
INTRODUCTION TO FILE-HANDLING
• Definition: Files are named-locations on a storage-medium where data can be
stored & retrieved by programs.
• Typically, the storage-medium is a disk.

FILE-HANDLING
• Definition: File-handling refers to the management and manipulation of files stored
on a storage-device.
Purpose
• Data Storage: Enables programs to store large amounts of data permanently.
• Data Retrieval: Facilitates retrieving stored-data for processing or display.
• Data Manipulation: Supports operations such as updating existing data,
appending new data, and deleting data.
• File Management: Includes file-operations such as opening, reading, writing, and
closing files.

FILE STREAM-CLASS
• Definition: File-stream-classes are specialized classes used for performing input
and output operations on files.
• File-stream-classes include
ifstream (Input File Stream)
ofstream (Output File Stream)
fstream (File Stream)

ifstream
• Purpose: Used to read data from files.
• Functionality: It allows opening a file and reading its contents, operating in input
mode by default.
• Example Usage:
ifstream inFile("example.txt");
string line;
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();

ofstream
• Purpose: Used to write data to files.
• Functionality: It allows creating a file or opening an existing file to write data to it,
operating in output mode by default.
• Example Usage:
ofstream outFile("example.txt");
outFile << "Hello, World!" << endl;
outFile.close();
fstream
• Purpose: Combines both input and output file-operations.
• Functionality: It allows both reading from and writing to files and can be used in
input, output, or both modes.
• Example Usage:
fstream file("example.txt", ios::in | ios::out);
string line;
while (getline(file, line)) {
cout << line << endl;
}
file << "New line added." << endl;
file.close();
Example Program: Demonstrating ifstream and ofstream
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Step 1: Write to the file using ofstream
ofstream outFile("example.txt");
outFile << "Hello, World!" << endl;
outFile.close();
cout << "Data written to file successfully." << endl;
// Step 2: Read from the file using ifstream
ifstream inFile("example.txt");
string line;
while (getline(inFile, line)) {
cout << "Read from file: " << line << endl;
}
inFile.close();
return 0;
}
Output:
Data written to file successfully.
Read from file: Hello, World!
COMPARISON OF ifstream, ofstream AND fstream

STANDARD CLASS-FUNCTIONS FOR FILE I/O


Definition
• These are functions provided by the file stream-classes to handle file-operations.
• The file stream-classes include `ifstream`, `ofstream`, `fstream`
Purpose
• To manage file input and output with built-in classes that handle file stream
operations.
• To enable the reading from and writing to files in a structured and efficient manner.
• Common Standard Class-functions for File I/O are listed in below table:
`open(const char* filename, ios_base::openmode mode)`
• Description: This function opens a file for reading, writing, or both.
• Parameters:
`const char* filename`: The name of the file to be opened.
`ios_base::openmode mode`: The mode in which the file should be opened
e.g., `ios::in`, `ios::out`, `ios::app`, `ios::binary`
• Example Usage:
ofstream outFile;
outFile.open("example.txt", ios::out); // Opens the file "example.txt" for writing
`close()`
• Description: This function closes the file that was opened.
Closing a file ensures that all data is properly saved and frees system-resources.
• Parameters: None.
• Example Usage:
outFile.close(); // Closes the file after writing
`read(char* buffer, streamsize size)`
• Description: This function reads a block-of-data from the file into a buffer.
This function is typically used for reading binary-data.
• Parameters:
`char* buffer`: The memory-location where the read data will be stored.
`streamsize size`: The number of bytes to read from the file.
• Example Usage:
ifstream inFile("data.bin", ios::binary);
inFile.read(data, sizeof(data)); // Reads 'sizeof(data)' bytes from the file into 'data'`write(const char*
buffer, streamsize size)`
• Description: This function writes a block-of-data from a buffer to a file.
This function is typically used for writing binary-data.
• Parameters:
`const char* buffer`: The memory-location containing the data to be written.
`streamsize size`: The no. of bytes to write to the file.
• Example Usage:
outFile.write(reinterpret_cast<const char*>(&number), sizeof(number));
// Writes 'sizeof(number)' bytes from 'number' to the file
Example Program: Demonstrating open(), close(), read() and write()
#include <iostream>
#include <fstream> // Include the fstream library for file I/O
using namespace std;
int main() {
// Part 1: Writing to a text file
ofstream outFile; // Create an ofstream object for writing to a file
outFile.open("example.txt", ios::out); // Opens the file "example.txt" for writing
if (outFile.is_open()) {
outFile << "Hello, this is a sample text file." << endl; // Write some text to the file
outFile.close(); // Closes the file after writing
cout << "Text file written successfully." << endl;
} else {
cout << "Failed to open the file for writing." << endl;
}
// Part 2: Reading from a binary file
ifstream inFile("data.bin",ios::binary);// Create ifstream object for reading from binary file
char data[100]; // Buffer to store the read data
if (inFile.is_open()) {
inFile.read(data, sizeof(data)); // Reads 'sizeof(data)' bytes from the file into 'data'
inFile.close(); // Closes the file after reading
cout << "Binary file read successfully." << endl;
} else {
cout << "Failed to open the binary file for reading." << endl;
}
// Part 3: Writing a number to a binary file
int number = 12345; // Example number to write to the file
outFile.open("data.bin", ios::binary | ios::out); // Opens file "data.bin" for binary writing
if (outFile.is_open()) {
outFile.write(reinterpret_cast<const char*>(&number), sizeof(number));
// Writes 'sizeof(number)' bytes from 'number' to the file
outFile.close(); // Closes the file after writing
cout << "Number written to binary file successfully." << endl;
} else {
cout << "Failed to open the binary file for writing." << endl;
}
return 0;
}
Output:
Text file written successfully.
Binary file read successfully.
Number written to binary file successfully.

FILE OPENING-MODES
• File opening-modes determine how a file is accessed and modified during fileoperations.
• They are specified when opening a file using file stream-classes (`ifstream`,
`ofstream`, `fstream`).
• Common file opening-modes are listed in below table:
TYPES OF FILES
TEXT-FILES
• Definition: Text-files store data as readable-characters and -strings.
• Features
- Data is in plain-text, with lines separated by newline-characters.
- Can be edited with any text-editor.
- Used for configuration-files, logs, and simple data-storage.
• Example Usage:
#include <fstream>
using namespace std;
int main() {
ifstream inFile("example.txt");
// Read from file
inFile.close();
}

BINARY-FILES
• Definition: Binary-files store data in a non-readable format, using bytes.
• Features
- Data is stored in binary-form, specific to the data-type.
- Cannot be easily edited with text-editors.
- Useful for storing images, executables, and complex data.
• Example Usage:
#include <fstream>
using namespace std;
int main() {
ofstream outFile("example.bin", ios::binary);
// Write to file
outFile.close();
}
TEXT-FILES VS. BINARY-FILES

You might also like