Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++ Passing File Stream Objects to Functions #226

Open Qingquan-Li opened 1 year ago

Qingquan-Li commented 1 year ago

Reference: Book: Starting Out with C++ from Control Structures to Objects, by Tony Gaddis, eighth edition

Concept: File stream objects may be passed by reference to functions.

The internal state of file stream objects changes with most every operation. They should always be passed to functions by reference to ensure internal consistency.

// This program demonstrates how file stream objects may
// be passed by reference to functions.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function prototypes
bool openFileIn(fstream &, string);
void showContents(fstream &);

int main()
{
    fstream dataFile;

    if (openFileIn(dataFile, "demofile.txt"))
    {
        cout << "File opened successfully.\n";
        cout << "Now reading data from the file.\n\n";
        showContents(dataFile);
        dataFile.close();
        cout << "\nDone.\n";
    }   
    else
       cout << "File open error!" << endl;

    return 0;
}

//***********************************************************
// Definition of function openFileIn. Accepts a reference   *
// to an fstream object as an argument. The file is opened  *
// for input. The function returns true upon success, false *
// upon failure.                                            *
//***********************************************************

bool openFileIn(fstream &file, string name)
{
   file.open(name, ios::in);
   if (file.fail())
      return false;
   else
      return true;
}

//***********************************************************
// Definition of function showContents. Accepts an fstream  *
// reference as its argument. Uses a loop to read each name *
// from the file and displays it on the screen.             *
//***********************************************************

void showContents(fstream &file)
{
   string line;

   while (file >> line)
   {
      cout << line << endl;
   }
}

demofile.txt:

Jones
Smith
Willis
Davis
$ g++ main.cpp -o main
$ ./main
File opened successfully.
Now reading data from the file.

Jones
Smith
Willis
Davis

Done.