EN Intro To Recursion by Slidesgo
EN Intro To Recursion by Slidesgo
with streams
We provide input to the executing program, and
What is I/O
the execution program returns the output.
Stream refers to the series of bytes sent
as input to the executing program and the
Operations?
sequence of bytes returned as output from the
executing program. In other words, streams are
nothing more than the sequential flow of data.
Syntax:
open( ‘File Name’, Mode)
There are several ways to open the file. Let's have a look at each of them in the table
below:
Writing to a File
In C++ programming, you use the stream insertion operator (<<) to write information to a file from
your program, just as you use that operator to output information to the screen. The only difference
is that instead of the cout object, you use an ofstream or fstream object.
Closing a File
To accomplish close operation, we use close(). When a C++ application terminates, it flushes all
streams, releases all allocated memory, and closes all open files. However, it is always a good habit
for a programmer to close all open files before terminating the program.
Syntax:
File_Name.close();
getline()
A line of text can be read or display effectively using the line oriented input/output functions getline()
and write().
The getline() function reads a whole line of text that ends with a newline character.This function can be
invoked by using the object cin as follows:
cin.getline(line,size);
This function call invokes the function getline() which reads character input into the variable line.The reading is
terminated as soon as either the newline character ‘\n’ is encountered or size-1 characters are read(whichever
occurs first).The newline character is read but not saved.instead it is replaced by the null character.
For example consider the following code:
char name[20];
cin.getline(name,20);
write()
The write() function displays an entire line and has the following form:
cout.write(line,size)
The first argument line represents the name of the string to be displayed and the second argument size indicates
the number of characters automatically when the null character is encountered.If the size is greater than the
length of line, then it displays beyond the bound of line
It is possible to concatenate two strings using the write() function.
Program:
#include <fstream> // open a file in read mode.
#include <iostream> ifstream input_file;
using namespace std;
input_file.open("temp.txt");
// C++ Files and Streams Example
int main () {
char data[100]; cout << "Reading from the file" << endl;
// open a file in write mode. input_file >> data;
ofstream output_file;
output_file.open("temp.txt"); // write the data at the screen.
cout << "Writing text to the file" << endl; cout << data << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
// Again, read the data from the file and display it.
// write inputted data into the file. input_file >> data;
output_file << data << endl; cout << data << endl;
cout << "Enter your age: "; // close the opened file.
cin >> data; input_file.close();
return 0;
// Again, write inputted data into the file.
output_file << data << endl; }
Conclusion:
In this way, we have seen various functions and operations which are
performed on the files and streams.
Thank You!!!
Presented by:
Wavhal Prathmesh