What capability does the fstream data type provide that the ifstream and ofstream data types do not?
Explanation of Solution
Header file used to perform file operations:
“fstream” (file stream) is the standard header file in C++ used to perform file operations such as file creation, write information to the file and read information from the file.
ofstream:
“ofstream” is a data type that represents output file stream used to create files and write information to files using output file stream object.
A file where the data are written is called as output file. When a program stores the output in a file, then it is called as output file.
ifstream:
“ifstream” is a data type that represents input file stream used to read information from files using input file stream object.
A file from which the data is read is called as input file. When a program gets input from the file, then it is called as input file.
Therefore, the data type “fstream” performs both read and write operation whereas the data type “ifstream” will perform only a read operation and the data type “ofstream” performs the write operation.
Example program:
The below code is an example for the “ifstream” usage to read information from file:
//Include the necessary header files
#include <iostream>
//Header file used to perform “file operations”
#include <fstream>
#include<string>
using namespace std;
// Main function
int main()
{
string str= "sample.txt";
//Create object for ofstream
ofstream out(str.data());
/*Write information to the file using ofstream object “out”*/
out << "Welcome to file operation";
//Close the file
out.close();
//Create object for ifstream
ifstream in(str.data());
/*Read the information from file until it reaches end of file*/
while (!in.eof())
{
in >> line;
cout << line;
}
//Newline
cout << endl;
//Close the file
in.close();
// Return statement
return 0;
}
Note: Write the information to the file using “ofstream” object.
Screenshot of “sample.txt” file
Note: Read the information from the file using “ifstream” object.
Screenshot of output file
Want to see more full solutions like this?
Chapter 12 Solutions
MyProgrammingLab - For Gaddis: Starting Out with C++ From Control Structures through Objects
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects (9th Edition)
Computer Science: An Overview (12th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Database Concepts (8th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Problem Solving with C++ (9th Edition)
- Do the similar names for different operations between iostream and fstream have any significance? Is it ever okay to repurpose a function name?arrow_forwardIs there more to the fact that many of the identical operations in iostream and fstream have the same names? Is there any other possible purpose for a function's name?arrow_forwardHow can you implement a custom InputStream for a specific data source or format?arrow_forward
- Describe the advantages of using a FileInputStream over a FileReader when dealing with binary data.arrow_forwardFind an incorrect statement. Group of answer choices Type iostream is used for handling both input and output file operations Type ifstream is used for handling input file operations Type ofstream is used for handling output file operations Type fstream is used for handling both input and output file operationsarrow_forwardwhat is inFile file stream object ?arrow_forward
- Under what circumstances is a file stream object’s ios::hardfail bit set? What member function reports the state of this bit?arrow_forwardWhat are the common file formats used for storing serialized objects, and what are their characteristics?arrow_forwardSymbian, Android, and the iPhone all employ the same file deletion mechanism.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage