PCC Unit 6 Notes + QUES BANK
PCC Unit 6 Notes + QUES BANK
🔹 1. Streams in C++
A stream is a flow of data (input/output).
Used for cin (input), cout (output), and file handling.
All stream classes are based on the base class ios.
🔹 2. Types of Streams
istream: for input (cin)
ostream: for output (cout)
iostream: for both input & output
ifstream: read from file
ofstream: write to file
fstream: read & write both
🔹 3. Steps for File Handling
1. Create stream object (ifstream, ofstream, fstream)
2. Open the file (file.open("file.txt"))
3. Read/Write
4. Close the file (file.close())
🔹 4. File Modes
Use with open() function:
ios::in – Read mode
ios::out – Write mode
ios::app – Append mode
ios::binary – Binary file mode
ios::trunc – Delete old content
ios::ate – Start at end of file
🔹 5. Basic File Programs
Write to file:
ofstream file("data.txt");
file << "Hello!";
file.close();
Read from file:
ifstream file("data.txt");
string line;
getline(file, line);
cout << line;
file.close();
🔹 6. File Error Handling Functions
eof() – End of file
fail() – Fail to read/write
bad() – Serious error
good() – Everything is OK
🔹 7. File Pointers
Used to move around in a file.
seekg() – set input (get) position
seekp() – set output (put) position
tellg() – get input position
tellp() – get output position
🔹 8. cerr vs clog
cerr – prints error messages, no buffer, appears immediately
clog – logs info, is buffered
🔹 9. Binary File I/O
Use ios::binary mode
file.write((char*)&obj, sizeof(obj));
file.read((char*)&obj, sizeof(obj));
Class Purpose
int main() {
ifstream file("data.txt"); // open file for reading
string line;
getline(file, line);
cout << line;
file.close();
return 0;
}
b) ofstream (Output File Stream)
ofstream is used to write data to a file. It is derived from the ostream class.
Example:
#include <fstream>
using namespace std;
int main() {
ofstream file("data.txt"); // open file for writing
file << "Hello, world!";
file.close();
return 0;
}
c) fstream (File Stream for Both Input & Output)
fstream is used when we want to read from and write to the same file. It combines ifstream
and ofstream.
Example:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
fstream file("data.txt", ios::in | ios::out); // open for both reading & writing
file << "New line";
file.seekg(0); // move read pointer to beginning
string text;
getline(file, text);
cout << text;
file.close();
return 0;
}
✅ Summary:
ifstream → read from file
ofstream → write to file
fstream → read and write both
5. Explain the concept of files with File Handling operations with example.
In C++, a file is used to store data permanently on disk.
File handling refers to the process of creating, opening, reading, writing, and closing a file
using C++ stream classes.
C++ provides the following classes for file handling:
ifstream – to read from a file (input)
ofstream – to write to a file (output)
fstream – to do both input and output
🔹 Basic File Handling Operations:
1. Create/Open a File
→ using ofstream, ifstream, or fstream
2. Write to a File
→ using << operator with ofstream or fstream
3. Read from a File
→ using >> or getline() with ifstream or fstream
4. Close the File
→ using .close() function to release file resources
📌 Example Program:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Writing to a file
ofstream outFile("demo.txt");
outFile << "Hello from C++ file!";
outFile.close();
// Reading from the file
ifstream inFile("demo.txt");
string text;
getline(inFile, text);
cout << text;
inFile.close();
return 0;
}
✅ Output:
Hello from C++ file!
💡 Summary:
Files help in storing data permanently.
C++ file operations include open, read, write, and close.
Stream classes (ifstream, ofstream, fstream) are used to handle files efficiently.
ofstream file("data.txt");
2. Writing to a File
o Using << operator
string text;
getline(file, text);
4. Closing a File
o Using .close() to release the file
file.close();
📌 Example Program:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Writing to a file
ofstream outFile("myfile.txt");
outFile << "File handling in C++";
outFile.close();
// Reading from a file
ifstream inFile("myfile.txt");
string line;
getline(inFile, line);
cout << line;
inFile.close();
return 0;
}
✅ Output:
File handling in C++
💡 Summary:
File handling allows storing and retrieving data using C++ streams.
Main operations: open, write, read, close
Makes programs more powerful by allowing data storage beyond runtime.
7. Explain following concepts with example. a) opening a file b) closing a file c) read
and write operation.
a) Opening a File
In C++, before reading from or writing to a file, we need to open it using file stream classes.
Files are opened using:
ifstream for reading
ofstream for writing
fstream for both reading and writing
We also use modes like ios::in, ios::out, ios::app, etc., while opening a file.
Syntax:
fstream file("filename.txt", ios::in | ios::out);
Example:
#include <fstream>
using namespace std;
int main() {
ofstream myFile("example.txt"); // File opened for writing
return 0;
}
If the file does not exist in write mode, it will be created. If it is opened in read mode and
doesn't exist, an error occurs.
b) Closing a File
After file operations, it's important to close the file to:
Free system resources
Ensure data is saved properly
We use .close() function.
Syntax:
myFile.close();
Example:
#include <fstream>
using namespace std;
int main() {
ofstream myFile("example.txt");
myFile << "Hello!";
myFile.close(); // File is closed
return 0;
}
c) Read and Write Operation
Writing to File:
We use ofstream or fstream and << operator.
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt");
file << "This is a test.";
file.close();
return 0;
}
Reading from File:
We use ifstream or fstream and >> or getline().
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("example.txt");
string line;
getline(file, line);
cout << line;
file.close();
return 0;
}
int main() {
int a = 5, b = 0;
if (b == 0) {
cerr << "Error: Cannot divide by zero.";
}
return 0;
}
Useful for debugging and alerting users about wrong input or failure in file/program
operations.