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 (9th 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)
- Do the similar names for different operations between iostream and fstream have any significance? Is it ever okay to repurpose a function name?arrow_forwardWrite a statement that defines an fstream object named dataFile and opens afile named salesfigures.txt for input. ( Note: The file should be opened withthe definition statement, not an open function call.)arrow_forwardwhat is ifstream file ?arrow_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_forwardDefine the term stream.arrow_forwardWhat are the different ios functions used in stream I/O?How are they different from manipulators? (C++)arrow_forward
- Computer Science The application should support integrity verification. That is, your client and server will calculate the checksum of each file after it is transferred and compare them to make sure data is transferred without any error in the network in c++.arrow_forward- a C program using pathconf that prints configuration values for filesarrow_forwardC++ You can mix input and output streams in the same statement?arrow_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_forwardDescribe the advantages of using a FileInputStream over a FileReader when dealing with binary data.arrow_forwardWhat is the usage of REAL10 type?arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning