0% found this document useful (0 votes)
11 views8 pages

RJ OOP Unit 4

The document explains the concept of streams in C++, detailing predefined streams like ifstream, ofstream, and fstream for file input and output operations. It includes sample C++ programs demonstrating file reading, error handling, overloaded stream operators, command line arguments, and creating files with a constructor. The document also outlines the structure for employee records and how to write them to a file.

Uploaded by

Sarita Dhakane
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)
11 views8 pages

RJ OOP Unit 4

The document explains the concept of streams in C++, detailing predefined streams like ifstream, ofstream, and fstream for file input and output operations. It includes sample C++ programs demonstrating file reading, error handling, overloaded stream operators, command line arguments, and creating files with a constructor. The document also outlines the structure for employee records and how to write them to a file.

Uploaded by

Sarita Dhakane
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/ 8

Q.1 What is stream ?

Explain various predefined streams used in C++ with their


purpose.
Ans. : Stream is basically a channel on which data flow from sender to receiver. Data
can be sent out from the program on an output stream or received into the program
on an input stream.

Q2. Comanly Use File Opening Function ?


Q.3 Explain the ofstream, ifstream, and fstream classes.
Ans. : C++ provides following classes to perform input and output of characters to
and from the files.

To perform file I/O, we need to include <fstream.h> in the program. It defines


several classes, including ifstream, ofstream and fstream. These classes are derived
from ios, so ifstream, ofstream and fstream have access to all operations defined by
ios. While using file I/O we need to
do following tasks -
1. To create an input stream, declare an object of type ifstream.
2. To create an output stream, declare an object of type ofstream.
3. To create an input/output stream, declare an object of type fstream.

Q.4 Write a C++ program to read


the contents of a text file.
Ans. :
#include<iostream>
cout<<'The Contents of the file
#include<fstream>
are..."<<endl;
#include<stdlib.h>
while(in)
using namespace std;
{
int main()
in.getline(Data,80);
{
cout< <'\n"< <Data;
ifstream in;
}
char Data[80];
in.close();
in.open("Sample.dat');
* return 0;
if (lin)
{
// Print an error and exit
cerr << "Sample.dat could not be
opened for readingl" << endl;
exit(1); .
}
Q.7. Write a C++ program to perform os.close(); //close the file
error handling with file I/O
ifstream is; //create input stream for
Ans. :-- > reading the contents from
#include <iostream> //file
#include <fstream> is.open("d:\\test.dat’, ios::binary);
using namespace std; if (lis)
#include <process.h> {
const int MAX = 10; cerr << "Could not open input file\n";
//Error Handling
int array1[MAX] = { -10,20,30,40,50 };
exit(1);
int array2[MAX];
}
int main()
cout << "Reading the contents from the
{
file ...\n";
ofstream os; //create output stream
is.read((char*)&array2,sizeof(array2));
os.open("d:\\test.dat", ios::trunc | //reading the contents in
ios::binary);//Opening file
//‘array2'
if (los) :
//another array
{
if (lis)
cerr << "Could not open output file\n";
{ cerr << "Could not read from file\n";
//Error Handling
//Error Handling
exit(1);
exit(1);
}
}
cout << "Writing the contents to the
for (int j = 0; j < MAX; j++) //check data
file...\n\n';
cout <<" "<< array2[j];
os.write((char*)&array1,sizeof(array1));//
writing 'array1' to file return 0;
if (los) }
{
cerr << "Could not write to file\n"; //Error
handling
exit(1);
}
Q.8 How do you declare an overloaded stream insertion and extraction
operator ?

Ans.: In C++ the output operation is denoted using << which is called - as insertion
operator. Similarly the input operation is denoted using >>
operator which is called as extraction operator. The C++ code for overloaded
insertion and extraction
//inserter function
ostream &operator< <(ostream &st,Point obj)
{
st <<obj.x << "," <obj.y <<"\n"; //outputting the values
retum st; //returning stream
}
//extractor function
istream &operator>>(istream &st, Point &obj)
{
cout << "Enter x: ";
st >> obj.x; __//inputting the values
cout << "Enter y: ";
st >> obj.y;
retum st;

Q.9 What is command line arguments in C++? Write a program for the same.?
Ans. : The command line arguments are the arguments that are passed to
the main function. These are represented as follows -
int main(int argc, char *argv[])

Here the arc represents the total number of arguments and argv is a an
array of characters that store the command line arguments.

The argy[0] is the name of the program, or an empty string if the name
is not available. After that, every element number less than arge is a
command line argument. You can use each argv element just like a
string.
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "Number of command-line arguments: " << argc << std::endl;
std::cout << "Command-line arguments:" << std::endl;
for (int i = 0; i < argc; i++) {
std::cout << "Argument " << i << ": " << argv[i] << std::endl;
} return 0;}
Q12. write a progrm in c++ to create file, // Open the file in write mode
and write record into it , every record
ofstream file("employee_records.txt");
contains employee name , id , salary,
store atleast 3 data .?? // Check if the file was opened
successfully
--- > #include <iostream>
if (!file) {
#include <fstream>
cout << "Error opening the file." <<
using namespace std;
endl;
struct Employee {
return 1;
string name;
}
int id;
// Write employee records to the file
double salary;
for (int i = 0; i < NUM_RECORDS; i++) {
};
file << "Name: " <<
int main() { employees[i].name << endl;
const int NUM_RECORDS = 3; file << "ID: " << employees[i].id <<
endl;
Employee employees[NUM_RECORDS];
file << "Salary: " <<
// Get employee records from the user
employees[i].salary << endl;
for (int i = 0; i < NUM_RECORDS; i++) {
file << endl; // Add a line break after
cout << "Enter details for Employee " each record
<< i + 1 << ":" << endl;
}
cout << "Name: ";
// Close the file
getline(cin, employees[i].name);
file.close();
cout << "ID: ";
cout << "Employee records have been
cin >> employees[i].id; written to the file." << endl;
cout << "Salary: "; return 0;
cin >> employees[i].salary; }
cin.ignore(); // Ignore the newline
character
cout << endl;
}
Q13.write function to create files using FileCreator file2("file2.txt");
constructer function??
file1.write("This is file 1.");
#include <iostream>
file2.write("This is file 2.");
#include <fstream>
return 0;
using namespace std;
}

class FileCreator {
public:
FileCreator(const string& filename) {
file.open(filename);
if (!file) {
cout << "Error creating the file." <<
endl;
}
}
void write(const string& content) {
if (file) {
file << content << endl;
}
}
~FileCreator() {
if (file.is_open()) {
file.close();
}
}
private:
ofstream file;
};
int main() {
FileCreator file1("file1.txt");

You might also like