basic_istream::operator>> in C++

basic_istream::operator data-lazy-src=

File handling is an important feature in C++ programming, and reading a file is a common task for data processing and configuration loading. You know that there are various methods to read a file efficiently. Here is another method to read a file instead of using the std::getline or eof(), i.e., the basic_istream::operator>> method in C++.

What is basic_istream::operator>> in C++?

The basic_istream::operator>> function in C++ is an overloaded extraction operator that is used to read the formatted input or text from an input stream by extracting the input data into a variable.

Syntax:

istream& operator>>(T& value);

Parameter:

  • T& value: It is a variable where the extracted data is stored.

Returns: The basic_istream::operator>> returns the istream object.

Read a File Using basic_istream::operator>> in C++

The basic_istream::operator>> in C++ is used to extract the texts or data from an input stream and store it in variables, but it does not read the entire line and stops at the spaces. It works well for basic data types like integers, floating-point numbers, and strings. It also handles the whitespace automatically and gives the output, which allows sequential and multiple parsing in C++ programming. The operator>> in this method is useful for structured data extraction to do the operations efficiently due to its sequential nature.

Example:

File name: sample.txt

Welcome to Intellipaat!

We are using basic_istream::operator>> in C++ to read the file.

Cpp

Output:

basic istream operator Example1

In this code, a file is opened and read by using the basic_istream::operator>> very easily. The while loop continues the file reading till the end and prints the output. Then, the file is closed.

Get 100% Hike!

Master Most in Demand Skills Now!

Another Example:

File name: sample.txt

Hello!

Let’s read a file.

Cpp

Output:

basic istream operator Example2

In this code, a file is opened and read by using the basic_istream::operator>> very easily. The while loop continues the file reading till the end and prints the output. Then, the file is closed.

Conclusion

You know that reading a file line-by-line in C++ is an essential part of processing data and file handling, and it can be done efficiently by using different methods. But the method using the  basic_istream::operator>> we have discussed here helps you to extract the data easily. Also, this method can be used in multiple parsing in C++ programming. So, by understanding this method, you can easily read a file line by line.

Some Other Methods to Read a File Line by Line in C++

  • Read from a Binary File in C++
  • eof() in C++
  • getline() Function in C++
  • istream::readsome() in C++
  • std::ifstream::get() in C++

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner