Reading in Information From a File in C++ Cin Statement
Definition of C++ Read File
As we are well aware that C++ is one of the most widely used languages and it provides many features that allows the programmer to perform multiple tasks easily. In C++, working on file operations is as like shooting fish in a barrel as working commonly on the console operations using the cin and cout. Practically working with files is very commonly used in order to shop the information permanently. fstream is the C++ library which is used to perform read and write file operations like iostream. The stream is nada but used equally the destination to inpt or output the data from 1 location to some other. There are several datatypes of this library which perform specific tasks of file operations. 'ifstream' is one of the data types used specifically for reading files in C++.
How to Read File in C++?
Equally mentioned in a higher place, 'ifstream' information type of 'fstream' library is used to read the files of C++. But before reading, there are several tasks which are performed sequentially like opening the file, reading and closing it. Unlike information types are used for the specific purpose. Let'due south empathise the datatypes of 'ifstream' mentioned below:
Data Type Name | Data Type Description |
ofstream | This data blazon is used to open up the file or write something in the file. It represents the output file stream. |
ifstream | This data type is used to read the data from the file. It represents the input file stream. |
fstream | This data blazon represents a normal file stream and can perform the tasks of both input and output file streams. It tin be used to open, create, read and write in the file. |
Given below is the step past step procedure to the file content in C++ :
1. Opening the Already Created File
In order to read the information from the file, nosotros need to get-go open it. The opening of the file is done using ofstream or fstream object of the file. Files in C++ can exist opened in different modes depending on the purpose of writing or reading. Hence, we demand to specify the fashion of file opening forth with the file proper noun.
At that place are basically 3 default modes which are used while opening a file in C++:
- ofstreamios: : out
- fstreamios: : in | ios: : out
- ofstreamios : :out
Syntax:
void open(filename, ios: : openmodemode_name);
2. Read the Data from The File
We can simply read the information from the file using the operator ( >> ) with the proper noun of the file. We need to employ the fstream or ifstream object in C++ in order to read the file. Reading of the file line past line can be done past simply using the while loop along with the role of ifstream 'getline()'.
three. Close the File
As we all know about the C++ memory management, equally the programme terminates, it frees all the allocated memory and the used resources. But however it is considered to be a good practice to close the file after the desired operation is performed.
Syntax:
void close();
Examples of C++ Read File
Below given are the few examples along with their outputs to demonstrate how the file read operation is performed in C++ :
Case #1
Code:
#include <iostream>
#include <fstream>
using namespace std;
intmain(){
char information[100];
// creating the file variable of the fstream information type for writing
fstreamwfile;
// opening the file in both read and write mode
wfile.open ("demo.txt", ios::out| ios::in );
// Asking the user to enter the content
cout<< "Please write the content in the file." <<endl;
// Taking the data using getline() function
cin.getline(information, 100);
// Writing the above content in the file named 'demp.txt'
wfile<< data <<endl;
// closing the file afterward writing
wfile.close();
//creating new file variable of information blazon 'ifstream' for reading
ifstreamrfile;
// opening the file for reading the content
rfile.open ("demo.txt", ios::in| ios::out );
// Reading the content from the file
rfile>> data;
cout<< information <<endl;
//closing the file after reading is done
rfile.shut();
render 0;
}
Output:
Explanation: In the higher up example, we take created two file variables of 'fstream' and 'ifstream' data blazon for writing and reading of the file respectively. In social club to read or write the file, nosotros need to start open up the file using the open() function and define its way. After opening, writing of the content in the file is done through the ( << ) operator and the file is closed afterwards writing using the close() function. Now the file is opened once more in club to read its content (using >> operator) and display information technology on the console (using cout part). In social club to release all the resources and free the allocated memory shut() role is used.
Instance #ii
When the file user is reading is non found.
Code:
#include<iostream>
#include<fstream>
using namespace std;
intmain()
{
char ch;
//creating the object of the information blazon 'istream'
ifstreamnew_file;
//opening the in a higher place file
new_file.open("demo1.txt",ios::in);
//checking whether the file is available or not
if(!new_file)
{
cout<<"Sorry the file you are looking for is not available"<<endl;
render -1;
}
// reading the whole file till the end
while (!new_file.eof())
{
new_file>>noskipws>>ch;
// press the content on the console
cout<<ch;
}
//closing the file afterwards reading
new_file.close();
return 0;
}
Output:
Explanation: In the to a higher place lawmaking, we are creating the object of the data blazon 'ifstream' in order to read the file named 'demo1.txt'. The file is opened using the open() part in read fashion using 'ios::in'. We take used the if and else statement to check whether the file is [resent or not. If the file is not found, a proper message is displayed on the console. Otherwise, it will read the whole file using the '>>' operator, and the content is printed on the console. In order to release all the resources and free the retentiveness, the close() function is used.
Conclusion
The above description clearly explains how the reading of files is done in C++ and the diverse information types of file stream used for specific operations. As working with files is very common in the applied earth, information technology is very of import to understand each and every file performance securely before using them.
Recommended Manufactures
This is a guide to C++ Read File. Here we also talk over the definition and how to read file in c++? forth with unlike examples and its lawmaking implementation. You may also have a look at the following articles to learn more –
- C++ Lambda Expressions
- C++ shuffle()
- C++ endl
- C++ Aggregation
Source: https://www.educba.com/c-plus-plus-read-file/
0 Response to "Reading in Information From a File in C++ Cin Statement"
Post a Comment