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
EBK STARTING OUT WITH C++ FROM CONTROL
Additional Engineering Textbook Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Modern Database Management
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Concepts Of Programming Languages
Introduction To Programming Using Visual Basic (11th Edition)
- Find the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(7): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': ')arrow_forwardFind the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] for i in range(6): daily_sales[i] = float(input('Enter the sales for ' \ + days_of_week[i] + ': '))arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it and normalize it? Give two references with your answer.arrow_forward
- What are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Consider that the database offline is not allowed since people are connected to it and personal data might be bridged and not secured. Provide three refernces with you answer.arrow_forwardShould software manufacturers should be tolerant of the practice of software piracy in third-world countries to allow these countries an opportunity to move more quickly into the information age? Why or why not?arrow_forwardI would like to know about the features of Advanced Threat Protection (ATP), AMD-V, and domain name space (DNS).arrow_forward
- I need help to resolve the following activityarrow_forwardModern life has been impacted immensely by computers. Computers have penetrated every aspect of oursociety, either for better or for worse. From supermarket scanners calculating our shopping transactionswhile keeping store inventory; robots that handle highly specialized tasks or even simple human tasks,computers do much more than just computing. But where did all this technology come from and whereis it heading? Does the future look promising or should we worry about computers taking over theworld? Or are they just a necessary evil? Provide three references with your answer.arrow_forwardWhat are the steps you will follow in order to check the database and fix any problems with it? Have in mind that you SHOULD normalize it as well. Describe in full, consider the following:• Taking the database offline is not allowed since people are connected to it.• Personal data might be bridged and not secured. Provide three refernces with you answerarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Fundamentals of Information SystemsComputer ScienceISBN:9781305082168Author:Ralph Stair, George ReynoldsPublisher:Cengage Learning