BPLCK205D Module 4
BPLCK205D Module 4
To use streams in C++, you need to include the appropriate header file. For instance, to use
input/output streams, you would include the iostream header file. This library provides the necessary
functions and classes to work with streams, enabling you to read and write data to and from files and
other I/O devices.
3. Write the syntax for opening the file and explain with an example?
Opening files in C++
To read or enter data to a file, we need to open it first. This can be performed with the help
of ‘ifstream’ for reading and ‘fstream’ or ‘ofstream’ for writing or appending to the file. All these
three objects have open() function pre-built in them.
Syntax:
open( FileName , Mode );
Here:
FileName – It denotes the name of file which has to be opened.
Mode – There different mode to open a file and it explained in this article.
Example:-
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream FileName;
FileName.open("FileName", ios::out);
if (!FileName)
{
cout<<"Error while creating the file";
}
Else
{
cout<<"File created successfully";
FileName.close();
4. Write the syntax for Writing in to the file and explain with an example?
Writing to File
Till now, we learned how to create the file using C++. Now, we will learn how to write data to
file which we created before. We will use fstream or ofstream object to write data into the file and
to do so; we will use stream insertion operator (<<) along with the text enclosed within the
double-quotes.
With the help of open() function, we will create a new file named ‘FileName’ and then we will
set the mode to ‘ios::out’ as we have to write the data to file.
Syntax:
FileName<<"Insert the text here";
5. Write the syntax for Reading from the file and explain with an example?
Reading from file in C++
Getting the data from the file is an essential thing to perform because without getting the data, we
cannot perform any task. But don’t worry, C++ provides that option too. We can perform the
Syntax:
FileName>>Variable;
Example:-
#include<iostream>
#include <fstream>
using namespace std;
int main() {
fstream FileName;
FileName.open("FileName.txt", ios::in);
if (!FileName) {
cout<<"File doesn’t exist.";
}
else {
char x;
while (1) {
FileName>>x;
if(FileName.eof())
break;
cout<<x;
}
}
FileName.close();
return 0;
}
Explanation of above code
Here we have an iostream library, which is responsible for input/output stream.
We also have a fstream library which is responsible for handling files.
Creating an object of the fstream class and named it ‘FileName’.
6. Write the syntax for closing the file and explain with an example?
Syntax:
FileName.close();
7. Write the C++ program to read a few lines and then display each word in a different line?
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
String Inputline, Outputline;
ofstream Obj1(“xyz.txt”);
cout<<”input:”<<endl;
while(1)
{
Cout<<”output:”;
ifstream Obj2(“xyz.txt”);
while(!obj2.eof())
{
Obj2>>Outputline<<endl;
Cout<<Outputline<<”/n”;
}
Obj2.close();
Return 0;
}
INPUT:
It was a Fight
For pride and ego
For one
It was a fight for
duty and self-respect for another
who won it at the end END
OUTPUT:
It
Was
A
Fight
…….
……
……
Who
8. Write the C++ program to read a few lines and then display line using unformatted I/O
methods ie get() and put()?
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
char ch;
ofstream Obj1(“xyz.txt”);
while(1)
{
Cin.get(ch);
If(ch==”$”)
Break;
Obj1<<ch<<endl;//writing to the file
}
Obj1.close();
ifstream Obj2(“xyz.txt”);
while(!obj2.eof())
{
Obj2>>Outputline<<endl;
Cout<<Outputline<<”/n”;
}
Obj2.close();
Return 0;
Output:
The Battle
Between x and y
Is
Between light and dark
9. Write the C++ program to read a few lines and then display line using unformatted I/O
methods ie getline()?
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
char a[80],b[80];
ofstream obj1(“xyz.txt”);
while(1)
{
cin.getline(a,80);
if(!strcmp(a,”end”))
break;
obj1<<a<<endl;
}
obj1.close();
ifstream obj2(“xyz.txt”);
while(!obj2.eof())
Input:
Imagination is
More important
Than Knowledge
end
Out put:
Imagination is more important that knowledge.
10. What is binary files?
Binary File Handling is a process in which we create a file and store data in its original
format. It means that if we stored an integer value in a binary file, the value will be treated as an
integer rather than text.
Binary files are mainly used for storing records just as we store records in a database. It
makes it easier to access or modify the records easily.
11. Write the syntax for opening the binary file?
A file stream object can be opened in one of two ways. First, you can supply a file name along
with an i/o mode parameter to the constructor when declaring an object:
ifstream myFile ("file name.txt", ios::in | ios::binary);
Alternatively, after a file stream object has been declared, you can call its open method:
ofstream myFile;
...
myFile.open ("filename.txt", ios::out | ios::binary);
12. Write the syntax for reading and writing the binary file?
Two member functions for ifstream and ofstream objects are useful in reading and writing both of
them have similar syntax.
Output:
Checking the xyz.txt file in C drive
Input:
Enter Rollno:1
Enter name:abc
Enter Address:xyz
Enter Rollno:2
Enter name:abcd
Enter Address:wxyz