Question

In: Computer Science

(1) Why do several functions in iostream and fstream have identical names? When is it advantageous...

(1) Why do several functions in iostream and fstream have identical names? When is it advantageous to reuse function names?

(2) Describe the five-step process of file I/O. Explain how to use ifstream and ofstream to open and close input and output files. Illustrate the process with the skeleton program provided in this section.

Please describe in details.

Solutions

Expert Solution


1.)

  • The iostream and fstream classes are inherited from the parent class,istream class.
  • Since C++ is a object-oriented programming language, it supports inheritance , function overloading and polymorphism features.
  • The inheritance feature reduces code reusability.
  • Function overloading feature to create one function with different parameter and argument list.
  • The most of the functions in the iostream and fstream seems to be similar.
  • The funcitons in the iostream are meant for reading and writing data from keyboard and write the data to console.
  • The funcitons in the fstream are meant for reading and writing data from secondary storage memory ,hard disk and write the data to files and store in computer hard disk or seconary storage device.


2.)
Five step process of file I/O.
Step1: Include fstream header file.
Step2: Declare
object of the fstream or ostream class to read or write the data to file.
Step3: Open the file using
open(filename) method.
Step4: Check if file exists using fail() function. If file not exists, then close the file using fclose() function
Step5: Close the file stream object using fclose() function.


Skeleton c++ program for opening input file stream for reading file and write the data to output file
stream.
#include <fstream>
using namespace std;
int main()
{
   //Open objects of ifstream and ofstream classes
   ifstream inFile;
   ofstream outFIle;
  
   //Open file for reading
   inFile.open("input.txt");
      
   //Check if file exists or not
   if (!inFile)
   {
           cerr << "Unable to open file datafile.txt";
           exit(1);   // call system to stop
   }

   //Open file for writing
   outFIle.open("output.txt");
   //Read data from file inFile and write to file output.txt
   while (inFile >> x)
   {
      outFile>>x>>endl;
   }

   //Close the file objects
   inFile.close();
   outFile.close();
   return 0;
}


Related Solutions

Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
1. Why do scientists think that double fertilization is advantageous to angiosperms?
  1. Why do scientists think that double fertilization is advantageous to angiosperms? 2. How can animals have a negative impact on angiosperm reproduction? 3. How can animals benefit angiosperm reproduction? 4. Which type of symmetry provides for greater specificity of pollen transfer, and why? 5. Describe two strategies that promote cross-pollination
Trigonometric Functions. Why do they have asymptotes and why do they have intervals?
Trigonometric Functions. Why do they have asymptotes and why do they have intervals?
#include<iostream> #include<string> #include<fstream> #include<vector> /*HW: Create two more functions, Modify the posted grade example that uses...
#include<iostream> #include<string> #include<fstream> #include<vector> /*HW: Create two more functions, Modify the posted grade example that uses dynamic arrays and file I/O to include the following: -Use vectors -read from values from the file and store them in the vector. Modify the function getGrades Extend the program to also include the following features: -A menu option that allows the user to add more students and there grades. Assume the number of quizzes stay the same based on the value read from...
What is a friend function? Do we have to use friend functions? Explain why or why...
What is a friend function? Do we have to use friend functions? Explain why or why not. c++
Explain why the functions diag, dim, length, and names can be as- signed new values (as...
Explain why the functions diag, dim, length, and names can be as- signed new values (as in diag(m)=pi). Explain why (or why not) they are useful.
Three firms have identical revenue and profit functions. Firm 1 is a private sector firm operated...
Three firms have identical revenue and profit functions. Firm 1 is a private sector firm operated by an​ owner-manager who wishes to maximize profit. Firm 2 is managed by an​ revenue-maximizing manager whose pay is proportional to the​ firm's revenue. Firm 3 is a​ government-owned firm that has been instructed to maximize the amount of​ employment, L, subject to the constraint that revenue must not be negative. Each of the three firms has a revenue function ​R(q)equals=120120qminus−22q squaredq2 and a...
Why do I keep getting the error "More columns than column names" when trying to import...
Why do I keep getting the error "More columns than column names" when trying to import the following data into R Studio?: PRICE   YEARSOLD   MILES   COLOR   TITLESTATUS   TRANSMISSION   4500   11   170000   SILVER   CLEAN   MANUAL           34590   1   2000   SILVER   CLEAN   AUTOMATIC   4500   14   203000   SILVER   CLEAN   AUTOMATIC   11990   6   53337   GRAY   CLEAN   AUTOMATIC   10490   8   36543   RED   CLEAN   AUTOMATIC   2800   19   208000   SILVER   CLEAN   MANUAL       1200   19   244000   SILVER   CLEAN   AUTOMATIC   2500   19   208000   BROWN   CLEAN   AUTOMATIC   2000  ...
Consider a competitive industry where all firms have identical cost functions C(y) = y^2 +1 if...
Consider a competitive industry where all firms have identical cost functions C(y) = y^2 +1 if y > 0 and C(0) = 0. Suppose that the demand curve for this industry is given by D(p) = 52 − p. (a) Find the individual supply curve of each firm. (b) Suppose there are n firms in the industry. Find the industry supply curve. (c) Find the price and quantity corresponding to a short-run market equilibrium assuming there are n firms in...
You have been pricing an MP3 player in several stores. Three stores have the identical price...
You have been pricing an MP3 player in several stores. Three stores have the identical price of $400. Each store charges 12 percent APR, has a 30-day grace period, and sends out bills on the first of the month. On further investigation, you find that store A calculates the finance charge by using the average daily balance method, store B uses the adjusted balance method, and store C uses the previous balance method. Assume you purchased the MP3 player on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT