Question

(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 andofstream to open and close input and output files. Illustrate the process with the skeleton program provided in this section.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution for the problem is provided below, please comment if any doubts:

(1).

Identical function name in iostream and fstream, since:

  • The objective of both the classes iostream and fstream are same, handles data i/o operations, iostream concentrated on direct input/output but fstream concentrated in file input/out. That is the operations to be performed are same only the way it is to be done is different. Same operations in different classed could be name same in C++ even though it will not suffer the name conflict since CPP supports function overloading.

It advantages to reuse, when:

  • It can be make advantageous when we have to perform the same operation on different variations of a class. It can be used to replace a function with another one that can do better work than other. The one name can’t tell anything about its functioning; it will be based on the class, the passed parameters, etc. So the same name can be used to increase the scope of functions.

(2)

Five-step process of file I/O:

  1. Include the proper header files in the beginning of the program, that is “#include <fstream>” along with other header files.
    • #include <fstream>
  2. Declare and define the local file stream variables to be used in the program, both input and output stream object needs to be declared if there is both input and output operations are there.
    • ifstream inputFileObject;
    • ofstream outputFileObject;
  3. Map the variables with the files, both input and out using ifstream and ofstream variables.
    • inputFileObject.open(inputFilenName)
    • outputFileObject.open(outputFilenName)
  4. Perform the operation on file, both write and read based on the program requirements.
    • data>> inputFilenName;
    • outputFileObject<<data;
  5. Close the file objects to make the file operations saved properly.
    • inputFileObject.close();
    • outputFileObject.close();
Add a comment
Know the answer?
Add Answer to:
(1) Why do several functions in iostream and fstream have identical names? When is it advantageous...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description:...

    Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description: For this project you will write a program to: a) read-in the 10 first names from a file (the file is a priori given to have exactly 10 entries, of a maximum length of 8 letters each) into a 2-dimensional character array, b) output the names to the terminal with each one preceded by a number indicating its original order in the list, c)...

  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace...

    How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) {    string line;    ifstream myfile("input.txt");    int i = 0;    if (myfile.is_open())    {        while (getline(myfile, line))        {            arr[i];            i++;        }        myfile.close();    } } void output(int arr[]) {    ofstream myfile("output.txt");    if (myfile.is_open())    {        int i = 0;...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • Intro C++ questions: Question 1: Assume #include <fstream> Given           ifstream fpIn; Write a statement to...

    Intro C++ questions: Question 1: Assume #include <fstream> Given           ifstream fpIn; Write a statement to open a file called carpools.txt and assign the pointer to fpIn. If the file cannot be found, then print out the message No such file and exit the execution of the program. Question 2: Meridian Corporation of Fremont is trying to decide whether to reward employees who carpool to work. The proposed plan is to pay each employee in the carpool $ 0.08 per...

  • In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading...

    In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward Provided output file: A single output file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT