C++ unit 4 Chap2
C++ unit 4 Chap2
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
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