input output streams in c++

Опубликовано: 30 Сентябрь 2024
на канале: Tarun Sir
1,255
34

Hey guys, in this video you will learn about the "Input output streams, file handling in c++".

I/O Stream
A stream is a sequence of data(bytes). A stream is used for the transportation of the data. A stream works as a medium to bring data into a program from a source or to send data from the program to a destination. The source can be a file or an input device, similarly the destination can be a file or an output device. We have following two types of streams:
• Input Stream: A stream which takes data from a file or input device and brings it into the program, is called as input stream.
• Output Stream: A stream which takes data from the program and send it to a file or output device is called as output stream.
Stream Classes
A class which represent a stream is called as stream class. The following diagram shows the IO stream classes.
Console Input Output Stream classes
The classes which are used to take input from the keyboard and used to write the data on monitor, are called as console input output stream classes.
ios class
• Base class for all other input output stream classes
• Defines constants which are necessary for formatted input output operations, file modes etc.

istream class
• Child class of ios
• Declares input functions like get(), getline(),read() etc.
• Also provides overloaded definitions of operator function like extraction.
ostream class
• Child class of ios
• Declares output functions like put(), write() etc.
• Also provides overloaded definitions of operator function insertion .

File Input Output Stream classes
The classes which are used to take input from the file and used to write the data on file, are called as file input output stream classes.
ifstream class
• Provides the input functionality
• Inherits functions like get(), getline(), extraction from istream
• Opens file with default read mode
ofstream class
• Defines output functionality
• Inherits functions like put(), write() , insertion from ostream
• Opens file with default write mode


fstream class
• Child class of both ifstream and ofstream
• Used for both file input and file output operations

File handling
A file is used to store data permanently on secondary storage devices. File handling allows us to read and write data from/to file. Every file handling program has three basic steps:
1. Opening a file
2. Reading/Writing data
3. Closing a file

Opening a file
A file can be opened in two ways
• Opening file using constructor
• Opening file using open() function

Opening file using constructor
The file can be opened while creating object of stream class.
When you open file using ifstream class then file is default opened for reading. If you open file using ofstream class then file is default opened for writing purpose.
ifstream fin("tarunsir.txt”);
The above statement creates fin object and open file tarunsir.txt for reading.
ofstream fout("tarunsir.txt”);
The above statement creates fout object and open file tarunsir.txt for writing.

Opening file using open() function
The ifstream or ofstream object can be used to open file by calling open() function.
ifstream fin;
fin.open("tarunsir.txt");

Closing the file
To close the file we use close() with stream object.

Q: WAP to input a number and write it on file.
Q: WAP to read a number from file and print it on screen.

www.tarunsir.com
www.cinstitute.org.in

Connect me on linkedin
  / tarrunverrma  

Follow on instagram
  / the_ultimate_coding_stuff  

Follow on twitter
  / tarrunverrma  

#tarunsir #cpptutorial #learnprogramming #coding #array