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
Starting Out with C++ from Control Structures to Objects (8th Edition)
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)
- What are the common file formats used for storing serialized objects, and what are their characteristics?arrow_forwardHow do serialization mechanisms handle cyclic references between objects?arrow_forwardc or c++ program for producer consumer example for unbounded buffer or when buffer size is n in operating systemarrow_forward
- in the FILE structure : What two fields we used to determine the size of the internal buffer allocated : Group of answer choices read_ptr and write_ptr base and end pointer flags and fileio all of the abovearrow_forwardWhat are the benefits of Request for Quotation (RFQ)?arrow_forwardPresentation slide JAVA Explain the concept of Java Streams, how they work, what benefits they provide Discuss the intermediate and terminal operations in Java Streamsarrow_forward
- You are to write a C++ function that given the name of a file passed as a string, the function will dynamically create an ofstream object. The function will open the file with the provided name. It will then make sure that the file properly opened. The function will then return a pointer to the ofstream object that was created.arrow_forwardWhat is the usage of REAL10 type?arrow_forwardwhat is invalid data?arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning