0% found this document useful (0 votes)
6 views

EN Intro To Recursion by Slidesgo

The document discusses file input/output (I/O) operations in C++ using streams. It covers opening, writing to, closing, and reading from files. Functions covered include open(), close(), getline(), write(), and stream insertion/extraction operators. An example program demonstrates writing user input to a file and then reading it back.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

EN Intro To Recursion by Slidesgo

The document discusses file input/output (I/O) operations in C++ using streams. It covers opening, writing to, closing, and reading from files. Functions covered include open(), close(), getline(), write(), and stream insertion/extraction operators. An example program demonstrates writing user input to a file and then reading it back.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Disk File I/O

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.

• Console I/O operation" refers to the input


and output operations between the running
program and devices such as the keyboard
and monitor. The "disk I/O
operation" refers to the input and output
operations between the executing application
and files.
 File Stream Operations Classes
 Opening a File
The open() method is used to open a file in order to read or write data to it. We read the file with the
'ifstream' library and write it with the 'ofstream' or 'fstream' library.

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; }

// close the opened file.


output_file.close();
Output:
Writing text to the file
Enter your name: John Snow
Enter your age: 21
Reading from the file
John Snow
21

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

You might also like