0% found this document useful (0 votes)
62 views10 pages

File Stream

The document discusses C++ files and streams, including how to read from and write to files using fstream library data types like fstream, ifstream, and ofstream. It provides examples of opening, writing, and reading from a file and using the getline() function to read input with a delimiter.

Uploaded by

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

File Stream

The document discusses C++ files and streams, including how to read from and write to files using fstream library data types like fstream, ifstream, and ofstream. It provides examples of opening, writing, and reading from a file and using the getline() function to read input with a delimiter.

Uploaded by

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

C++ Files and Streams

Unit V
C++ Files and Streams
• In C++ programming we are using the iostream standard
library, it provides cin and cout methods for reading
from input and writing to output respectively.

• To read and write from a file we are using the standard


C++ library called fstream.

• Data types define in fstream library is:


Data Types & Description
• fstream: It is used to create files, write
information to files, and read information
from files.
• ifstream: It is used to read information from
files.
• ofstream: It is used to create files and write
information to the files.
C++ FileStream example: writing to a file

#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream filestream("testout.txt");
if (filestream.is_open())
{
filestream << "Welcome \n";
filestream << "C++ Tutorial.\n";
filestream.close();
}
else cout <<"File opening is fail.";
return 0;
}
C++ FileStream example: reading from a file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string srg;
ifstream filestream("testout.txt");
if (filestream.is_open())
{
while ( getline (filestream,srg) )
{
cout << srg <<endl;
}
filestream.close();
}
else {
cout << "File opening is fail."<<endl;
}
return 0;
}
C++ getline()

• The cin is an object which is used to take input


from the user but does not allow to take the
input in multiple lines.
• To accept the multiple lines, we use the
getline() function.
• It is a pre-defined function defined in
a <string.h> header file used to accept a line
or a string from the input stream until the
delimiting character is encountered.
Syntax of getline() function:

istream& getline( istream& is, string& str, char delim );


The above syntax contains three parameters, i.e., is, str, and delim.
Where,
is: It is an object of the istream class that defines from where to
read the input stream.
str: It is a string object in which string is stored.
delim: It is the delimiting character.

Return value
This function returns the input stream object, which is passed as a
parameter to the function.
Example
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string profile; // variable declaration
std::cout << "Enter your profile :" << std::endl;
getline(cin,profile,' '); // implementing getline() function with a
delimiting character.
cout<<"\nProfile is :"<<profile;
}
Output
• In the above code, we take the user input by
using getline() function, but this time we also
add the delimiting character('') in a third
parameter. Here, delimiting character is a space
character, means the character that appears
after space will not be considered.
• Output
• Enter your profile : Software Developer
• Profile is: Software
Delimiter
• A delimiter is a sequence of one or more characters
for specifying the boundary between separate,
independent regions in plain text, mathematical
expressions or other data streams.
• An example of a delimiter is the comma character,
which acts as a field delimiter in a sequence of
comma-separated values.
• Delimiters are used as separators between the
characters or words in a string so that different
results can get separated by the delimiter.

You might also like