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)
- 14. Show that the language L= {wna (w) < Nь (w) < Nc (w)} is not context free.arrow_forward7. What language is accepted by the following generalized transition graph? a+b a+b* a a+b+c a+b 8. Construct a right-linear grammar for the language L ((aaab*ab)*).arrow_forward5. Find an nfa with three states that accepts the language L = {a^ : n≥1} U {b³a* : m≥0, k≥0}. 6. Find a regular expression for L = {vwv: v, wЄ {a, b}*, |v|≤4}.arrow_forward
- 15. The below figure (sequence of moves) shows several stages of the process for a simple initial configuration. 90 a a 90 b a 90 91 b b b b Represent the action of the Turing machine (a) move from one configuration to another, and also (b) represent in the form of arbitrary number of moves.arrow_forward12. Eliminate useless productions from Sa aA BC, AaBλ, B→ Aa, C CCD, D→ ddd Cd. Also, eliminate all unit-productions from the grammar. 13. Construct an npda that accepts the language L = {a"b":n≥0,n‡m}.arrow_forwardYou are given a rope of length n meters and scissors that can cut the rope into any two pieces. For simplification, only consider cutting the rope at an integer position by the meter metric. Each cut has a cost associated with it, c(m), which is the cost of cutting the rope at position m. (You can call c(m) at any time to return the cost value.) The goal is to cut the rope into k smaller pieces, minimizing the total cost of cutting. B Provide the pseudo-code of your dynamic programming algorithm f(n,k) that will return the minimum cost of cutting the rope of length n into k pieces. Briefly explain your algorithm. What is the benefit of using dynamic programming for this problem? What are the key principles of dynamic programming used in your algorithm?arrow_forward
- Determine whether each of the problems below is NP-Complete or P A. 3-SAT B. Traveling Salesman Problem C. Minimum Spanning Tree D. Checking if a positive integer is prime or not. E. Given a set of linear inequalities with integer variables, finding a set of values for the variables that satisfies all inequalities and maximizes or minimizes a given linear objective function.arrow_forward1. Based on our lecture on NP-Complete, can an NP-Complete problem not have a polynomial-time algorithm? Explain your answer. 2. Prove the conjecture that if any problem in NP is not polynomial-time solvable, then no NP-Complete problem is polynomial-time solvable. (You can't use Theorem 1 and 2 directly) 3. After you complete your proof in b), discuss how this conjecture can be used to solve the problem of whether P=NP.arrow_forwardBased on our lectures and the BELLMAN-FORD algorithm below, answer the following questions. BELLMAN-FORD (G, w, s) 1 INITIALIZE-SINGLE-SOURCE (G, s) 2 for i = 1 to |G. VI - 1 3 4 5 6 7 8 for each edge (u, v) = G.E RELAX(u, v, w) for each edge (u, v) = G.E if v.d> u.d+w(u, v) return FALSE return TRUE 1. What does the algorithm return? 2. Analyze the complexity of the algorithm.arrow_forward
- (Short-answer) b. Continue from the previous question. Suppose part of the data you extracted from the data warehouse is the following. Identify the missing values you think exist in the dataset. Use Column letter and Row number to refer to each missing value in the dataset. Please write down how you want to address each particular missing value (you can group them if they receive same treatment). For imputation, you do not need to calculate the exact imputed values but just describe what kind of value you want to use to impute.arrow_forwardPlease original work Locate data warehousing solutions offered by IBM, Oracle, Microsoft, and Amazon Compare and contrast the capabilities of each solution and provide several names of some organizations that utilize each of these solutions. Please cite in text references and add weblinksarrow_forwardNeed Help: Which of the following statements about confusion matrix is wrong A) Confusion matrix is a performance measure for probability prediction techniques B) Confusion matrix is derived based on classification rules with cut-off value 0.5 C) Confusion matrix is derived based on training partition to measure a model’s predictive performance D) None of the abovearrow_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