BIT153 ยท 2081 ยท 10 marks
Define stream.How do you read and write binary files?Write a program to read an integer from the user and if the integer is less than 100 then write it in file named "SMALL.TXT" otherwise write in file named "LARGE.TXT".[2+3+5]
Define stream.How do you read and write binary files?Write a program to read an integer from the user and if the integer is less than 100 then write it in file named "SMALL.TXT" otherwise write in file named "LARGE.TXT".[2+3+5]
Quick answer
A stream is an abstraction that represents a flow of data between a program and an input/output device (such as a keyboard, monitor, disk file, etc.).
Streams, Binary File I/O, and File Writing Program
1. Definition of Stream (2 marks)
- A stream acts as a logical interface between the program and the actual I/O device.
- In C++, streams are implemented through a hierarchy of classes defined in headers like
<iostream>and<fstream>. - There are two main types:
- Input Stream: Data flows into the program (e.g., reading from a file or keyboard).
- Output Stream: Data flows out of the program (e.g., writing to a file or screen).
Key stream classes:
ifstream(input file stream),ofstream(output file stream),fstream(both input and output).
2. Reading and Writing Binary Files (3 marks)
Binary files store data in the same format as it is stored in memory (raw bytes), unlike text files which store data as readable characters.
Opening a Binary File
Use the ios::binary flag along with the file mode:
ifstream fin("file.dat", ios::in | ios::binary); // reading
ofstream fout("file.dat", ios::out | ios::binary); // writing
Writing to a Binary File
Use the write() member function:
fout.write((char*) &variable, sizeof(variable));
- The first argument is a pointer to the data cast to
char*. - The second argument is the number of bytes to write.
Reading from a Binary File
Use the read() member function:
fin.read((char*) &variable, sizeof(variable));
Example
#include <fstream>
using namespace std;
int main() {
int num = 42;
// Write binary
ofstream fout("data.bin", ios::out | ios::binary);
fout.write((char*) &num, sizeof(num));
fout.close();
// Read binary
int readNum;
ifstream fin("data.bin", ios::in | ios::binary);
fin.read((char*) &readNum, sizeof(readNum));
fin.close();
return 0;
}
3. Program: Write Integer to SMALL.TXT or LARGE.TXT (5 marks)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int num;
// Read integer from user
cout << "Enter an integer: ";
cin >> num;
if (num < 100) {
// Write to SMALL.TXT
ofstream fout("SMALL.TXT", ios::app); // ios::app to append
if (!fout) {
cout << "Error opening SMALL.TXT!" << endl;
return 1;
}
fout << num << endl;
fout.close();
cout << num << " has been written to SMALL.TXT" << endl;
} else {
// Write to LARGE.TXT
ofstream fout("LARGE.TXT", ios::app); // ios::app to append
if (!fout) {
cout << "Error opening LARGE.TXT!" << endl;
return 1;
}
fout << num << endl;
fout.close();
cout << num << " has been written to LARGE.TXT" << endl;
}
return 0;
}
Sample Output
Enter an integer: 45
45 has been written to SMALL.TXT
Enter an integer: 250
250 has been written to LARGE.TXT
Explanation of Key Steps
| Step | Description |
|---|---|
cin >> num | Reads integer from the user |
if (num < 100) | Checks the condition |
ofstream fout("SMALL.TXT", ios::app) | Opens SMALL.TXT in append mode |
ofstream fout("LARGE.TXT", ios::app) | Opens LARGE.TXT in append mode |
if (!fout) | Checks if file opened successfully |
fout << num | Writes the integer to the file |
fout.close() | Closes the file after writing |
Note:
ios::app(append mode) is used so that previously written values are not overwritten each time the program runs. You may also useios::outif overwriting is acceptable.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.