Oop Unit V
Oop Unit V
#include<iostream>
#include<fstream>
RSCOEP Department of Computer Engineering Prof.S.U.Puri 163
using namespace std;
int main()
{
int count = 0;
string line;
ifstream file("abc.txt");
while (getline(file, line))
count++;
cout << "Numbers of lines in the file : " << count << endl;
return 0;
}
4. Develop a C++ program to read content of file abc.txt 4 S-24
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inputFile("abc.txt"); // Check if the file was opened
successfully
if (inputFile.is_open()) {
string line; // Read the file line by line
while (getline(inputFile, line))
{
// Print each line to the console
cout << line << endl;
}
inputFile.close();
}
else {
//cerr << "Error: Could not open file 'abc.txt'." << endl;
cout << "Error:'abc.txt' File not found." << endl;
}
return 0;
}
5. Give the syntax and use of fclose() fuction. 2 W-23
6. Write a C++ program for write into a file using file operations. 4 W-23
#include <iostream>
#include <string>
int main() {
if (!outputFile) {
return 1;
return 0;
7. Write a C++ program to copy data from one file to another 4 W-23
#include <iostream>
#include <fstream>
int main() {
ifstream inputFile("source.txt");
ofstream outputFile("destination.txt");
if (!inputFile) {
return 1;
if (!outputFile) {
return 1;
char ch;
while (inputFile.get(ch)) {
inputFile.close();
outputFile.close();
return 0;
What is a File?
File Operations
Reading from a file: Retrieves data from the file and stores it in
memory.
Closing a file: Terminates the connection between the program and the
file, releasing system resources.
Seeking: Moves the file pointer to a specific position within the file.
get() and put() are member functions of the istream and ostream classes
respectively, which are used for input and output operations. They are
typically used for character-based input and output.
get() function
Syntax: istream_object.get(char_variable)
Description: Reads a single character from the input stream and stores
it in the specified character variable.
Return value: Returns the input stream object itself, allowing for
chaining.
put() function
Syntax: ostream_object.put(char_value)
Return value: Returns the output stream object itself, allowing for
chaining.
#include <iostream>
#include <fstream>
int main() {
ofstream outputFile("example.txt");
outputFile << "This is some data to be written to the file." << endl;
outputFile.close();
return 0;
(i) ios : : in
(ii) ios : : out
File modes determine how a file is opened when using file streams.
They specify the type of operations that can be performed on the file.
RSCOEP Department of Computer Engineering Prof.S.U.Puri 168
Here are the common file modes in C++:
Basic Modes
ios::out: Opens a file for output operations. If the file doesn't exist, it's
created.
ios::app: Opens a file for appending data at the end. If the file doesn't
exist, it's created.
C++ provides a set of stream classes for handling input and output
operations. These classes are organized in a hierarchy with ios as the
base class.
ios: This is the base class for all stream classes. It provides
fundamental functionalities for input and output operations.
13. Develop c++ program to open and read content of file also write 4 W-22
#include <iostream>
#include <fstream>
#include <string>
int main() {
string line;
if (inputFile.is_open()) {
inputFile.close();
} else {
return 1;
if (outputFile.is_open()) {
outputFile.close();
} else {
return 1;
return 0;
#include <iostream>
#include <fstream>
ifstream inputFile("file.txt");
if (!inputFile) {
return 1;
char ch;
while (inputFile.get(ch)) {
if (inputFile.eof()) {
} else {
inputFile.close();
return 0;
15 List c++ stream classes along with their function. (any two classes) 2 S-22
ios::app
Purpose: Opens a file for appending data.
Behavior: When a file is opened with this flag, any data written to the
file is added to the end of the existing content. If the file doesn't exist,
it's created.
ios::in
Purpose: Opens a file for input operations.
Behavior: Allows reading data from an existing file. The file pointer is
positioned at the beginning of the file.
17. Write a C++ program to copy the contents of a source file student 4 S-22
#include <iostream>
#include <fstream>
int main() {
ifstream sourceFile("student1.txt");
ofstream destinationFile("student2.txt");
if (!sourceFile) {
return 1;
if (!destinationFile) {
return 1;
char ch;
while (sourceFile.get(ch)) {
sourceFile.close();
destinationFile.close();
return 0;
#include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream inputFile("file.txt");
int count = 0;
if (inputFile.is_open()) {
count++;
inputFile.close();
} else {
return 0;
19. Write a program that copies contents of one file into another file. 6 W-19
#include <iostream>
#include <fstream>
int main() {
ifstream sourceFile;
ofstream destinationFile;
char ch;
sourceFile.open(sourceFileName, ios::in);
if (!sourceFile) {
return 1;
destinationFile.open(destinationFileName, ios::out);
if (!destinationFile) {
sourceFile.close();
return 1;
while (sourceFile.get(ch)) {
sourceFile.close();
destinationFile.close();
return 0;
20. Write a C++ program to count number of spaces in text file 4 S-19
#include <iostream>
#include <fstream>
int main() {
ifstream inputFile("file.txt");
char ch;
int spaceCount = 0;
if (inputFile.is_open()) {
while (inputFile.get(ch)) {
spaceCount++;
inputFile.close();
} else {
return 0;
21. Write a C++ program to append data from abc .txt to xyz .txt file. 6 S-19
#include <iostream>
#include <fstream>
int main() {
ifstream sourceFile("abc.txt");
if (!sourceFile) {
return 1;
if (!destinationFile) {
return 1;
char ch;
while (sourceFile.get(ch)) {
sourceFile.close();
destinationFile.close();
return 0;
22. Write a C++ program to write ‘Welcome to poly’ in a file. Then read 6 W-18
#include <iostream>
#include <fstream>
int main() {
// Write to file
ofstream outputFile("welcome.txt");
if (outputFile.is_open()) {
outputFile.close();
} else {
return 1;
ifstream inputFile("welcome.txt");
string data;
if (inputFile.is_open()) {
getline(inputFile, data);
inputFile.close();
cout << "Data from file: " << data << endl;
} else {
return 1;