0% found this document useful (0 votes)
9 views14 pages

PCC Unit 6 Notes + QUES BANK

The document provides detailed notes on file handling in C++ using streams, including types of streams (istream, ostream, fstream), file operations (opening, reading, writing, closing), and error handling functions (eof, fail, bad, good). It includes examples of writing to and reading from files, as well as explanations of file pointers and their functions (seekg, tellg, seekp, tellp). Additionally, it distinguishes between cerr and clog for error messages and logging.

Uploaded by

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

PCC Unit 6 Notes + QUES BANK

The document provides detailed notes on file handling in C++ using streams, including types of streams (istream, ostream, fstream), file operations (opening, reading, writing, closing), and error handling functions (eof, fail, bad, good). It includes examples of writing to and reading from files, as well as explanations of file pointers and their functions (seekg, tellg, seekp, tellp). Additionally, it distinguishes between cerr and clog for error messages and logging.

Uploaded by

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

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));

1. Explain the concept of stream and files with example.


In C++, a stream is a flow of data — either into the program (input) or out of the program
(output). It acts like a channel between the program and an input/output device or file.
There are two main types of streams:
 Input stream: used to read data (e.g., cin)
 Output stream: used to write data (e.g., cout)
C++ uses special classes to handle files:
 ifstream – to read from a file
 ofstream – to write to a file
 fstream – to read and write both
📌 Example: Writing and Reading from a File
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Writing to file
ofstream outFile("example.txt");
outFile << "Hello from stream!";
outFile.close();
// Reading from file
string line;
ifstream inFile("example.txt");
getline(inFile, line);
cout << line;
inFile.close();
return 0;
}
✅ Explanation:
 ofstream creates and writes data to the file.
 ifstream reads data from the file.
 Data flows through streams, just like water flows through a pipe.

2. Explain the concept of stream classes with example.


In C++, stream classes are used to handle input and output (I/O) operations through
streams. A stream is simply the flow of data — either from input devices or to output devices
or files.
C++ provides several stream classes, based on the base class ios (input-output stream).
🔹 Main Stream Classes:

Class Purpose

istream Input from standard devices (cin)

ostream Output to standard devices (cout)

iostream Both input and output


Class Purpose

ifstream Input from files

ofstream Output to files

fstream Input and output from files

📌 Example: Using Stream Classes


#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileOut("demo.txt"); // ostream class used to write
fileOut << "Stream class example!";
fileOut.close();
ifstream fileIn("demo.txt"); // istream class used to read
string text;
getline(fileIn, text);
cout << text; // ostream used to display output
fileIn.close();
return 0;
}
✅ Explanation:
 ofstream is derived from ostream, used for file output.
 ifstream is derived from istream, used for file input.
 cout is from ostream, and cin is from istream.
 All stream classes inherit from the base class ios.

3. Explain following concepts with example. a) istream b) ostream c) iostream


a) istream
istream is the input stream class in C++, used to read data from input devices like the
keyboard.
👉 It handles objects like cin.
Example:
int age;
cin >> age; // cin is an object of istream
b) ostream
ostream is the output stream class in C++, used to display output on the screen or write to
files.
👉 It handles objects like cout.
Example:
cout << "Hello, world!"; // cout is an object of ostream
c) iostream
iostream is a class that combines both istream and ostream, so it supports both input and
output operations.
👉 It is used when you need to do both reading and writing in a program.
Example:
int age;
cout << "Enter age: ";
cin >> age;
cout << "You entered: " << age;
✅ Summary:
 istream → for input (cin) , iostream → for output (cout) ,iostream → for both input and
output

4. Explain following concepts with example. a) ifstream b) ofstream c) fstream


a) ifstream (Input File Stream)
ifstream is used to read data from a file. It is derived from the istream class.
Example:
#include <fstream>
#include <iostream>
using namespace std;

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.

6. Explain File Handling operations with example.


File handling in C++ means performing actions like creating, opening, reading, writing, and
closing files to store or access data permanently.
C++ provides these file stream classes for file handling:
 ifstream – for reading (input)
 ofstream – for writing (output)
 fstream – for both reading and writing
🔹 File Handling Operations:
1. Opening a File
o Using constructors or .open() function

ofstream file("data.txt");
2. Writing to a File
o Using << operator

file << "Hello!";


3. Reading from a File
o Using >> or getline()

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;
}

8. Explain error handling in file I/O.


File errors can occur due to:
 File not existing
 Wrong file mode
 Reading/writing to unopened files
C++ provides functions like:
 bad() – severe error
 fail() – input/output operation failed
 eof() – end of file reached
 good() – no error
These help in checking the file status and handling problems during file operations.
Example:
if (file.fail()) {
cout << "File read failed";
}
These checks prevent program crashes and help in debugging.

9. Explain built in functions to handle the file errors.


1) bad()
Checks for non-recoverable errors. Returns true if the operation failed completely.
if (file.bad()) {
cout << "Bad error occurred.";
}
2) fail()
Returns true if the last operation failed (like wrong data type).
if (file.fail()) {
cout << "Operation failed.";
}
3) good()
Returns true if no error has occurred.
if (file.good()) {
cout << "Operation successful.";
}
4) eof()
Returns true if the end of file is reached.
while (!file.eof()) {
file >> data;
}
These functions help in safely checking and controlling file operations.

10. Explain the concept of file pointers.


File pointers are used to track the current position in a file:
 get pointer: for reading
 put pointer: for writing
They are automatically moved during operations, but we can manually control them using
functions like seekg(), seekp(), tellg(), and tellp().
Why use them?
 To move to a specific position in the file
 To know current position
Example:
file.seekg(0, ios::beg); // Move read pointer to beginning

11. Explain following concepts: a) seekg, b)tellg, c)seekp , d)seekg.


a) seekg()
Moves the get (read) pointer.
file.seekg(10, ios::beg); // 10 bytes from start
b) tellg()
Tells the current position of get pointer.
int pos = file.tellg();
c) seekp()
Moves the put (write) pointer.
file.seekp(5, ios::cur); // 5 bytes forward from current
d) tellp()
Tells the current position of put pointer.
int pos = file.tellp();
These help in random file access and efficient data manipulation.

12. Explain the concept cerr with example .


cerr is used to display error messages. It is unbuffered, meaning the message appears
immediately without delay.
Syntax:
cerr << "Error message";
Example:
cpp
Copy code
#include <iostream>
using namespace std;

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy