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
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)
- Q2/find the transfer function C/R for the system shown in the figure Re དarrow_forwardPlease original work select a topic related to architectures or infrastructures (Data Lakehouse Architecture). Discussing how you would implement your chosen topic in a data warehouse project Please cite in text references and add weblinksarrow_forwardPlease original work What topic would be related to architectures or infrastructures. How you would implement your chosen topic in a data warehouse project. Please cite in text references and add weblinksarrow_forward
- What is cloud computing and why do we use it? Give one of your friends with your answer.arrow_forwardWhat are triggers and how do you invoke them on demand? Give one reference with your answer.arrow_forwardDiscuss with appropriate examples the types of relationships in a database. Give one reference with your answer.arrow_forward
- Determine the velocity error constant (k,) for the system shown. + R(s)- K G(s) where: K=1.6 A(s+B) G(s) = as²+bs C(s) where: A 14, B =3, a =6. and b =10arrow_forward• Solve the problem (pls refer to the inserted image)arrow_forwardWrite .php file that saves car booking and displays feedback. There are 2 buttons, which are <Book it> <Select a date>. <Select a date> button gets an input from the user, start date and an end date. Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, it books cars for specific dates, with bookings saved. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. In the booking.json file, if the Car ID and start date and end date matches, it fails Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert).arrow_forward
- Write .php file with the html that saves car booking and displays feedback. Booking should be in the .json file which contains all the bookings, and have the following information: Start Date. End Date. User Email. Car ID. There are 2 buttons, which are <Book it> <Select a date> Book it button can be pressed only if the start date and ending date are chosen by the user. If successful, book cars for specific dates, with bookings saved. If the car is already booked for the selected period, a failure message should be displayed, along with a button to return to the homepage. Use AJAX: Save bookings and display feedback without page refresh, using a custom modal (not alert). And then add an additional feature that only free dates are selectable (e.g., calendar view).arrow_forward• Solve the problem (pls refer to the inserted image) and create line graph.arrow_forwardwho started the world wide webarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning