Hi there,

Say I have created an ifstream object in main():

ifstream [B]inData[/B];                        // Create new input file stream object
    inData.open( "input.dat" );

How could I then pass this object to a function, instead of having to recreate it there? I.e. what would the argument and parameter look like?

Thanks :)

you would just pass it into the function as an ifstream type just like you declared it:

#include <iostream.h>
#include <fstream.h>

void displayFileContents(ifstream file)
{
    string output;

    while(getline(file,output))
    {
        std::cout << output;
    }
}

int main()
{
    ifstream myfile;
    myfile.open("file.txt");
    displayFileContents(myfile);
   return 0;
}

that code should open a file then read it's contents, but i haven't tested it lol, just typed it here. Hope that helps your problem... If not you can just pm me or something.

Just make the parameter of function of type istream and pass by reference.
Maybe an example would help:

int print_the_istream(std::istream& the_stream)
{
    if (! the_stream)
        return 0;// error, return false

    std::string buff;
    while (!the_stream.eof())
    {
        the_stream>>buff;
        std::cout<<buff;
    }
    return 1;// return true.Everything is fine
}
//usage:
int main ()
{
    std::ifstream inData;                        // Create new input file stream object
    inData.open( "aboutYou.txt" );
    if ( ! print_the_istream(inData))
        std::cout<<"Cannot read the stream";
}

[edit:]Coder++:
what are iostream.h and fstream.h? These were the old headers. ISO strictly reccoments to use fstream and iostream instead.
Your code will not work because you have not passed istream object by reference. You have passed by value.

Thanks for these very helpful replies... problem solved!

Hi again,

After doing what you said, I bumped into another problem. My code is as follows:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int findRows( istream &inFile );
void readData( istream &inFile );

int main()
{
    
    string line;                            // Create new string to store each line of data
    ifstream inData;                        // Create new input file stream object
    inData.open( "input.dat" );

    const int arrayRows = findRows( inData );     // Find the number of rows to use in array initialization
    cout << "Number of rows of data: " << arrayRows << endl << endl;

    const int arrayColumns = 11;            // Always 11 columns!

    double data[ arrayRows ][ arrayColumns ];       

    readData( inData );
    inData.close();

}

// Function to calculate the number of rows in the data
int findRows( istream &inFile )
{
    int Rows = 0;
    string Line;

    if ( !inFile )
    {
        cout << "Error in opening file" << endl;
    }

    // Find the number of rows in the data
    while( getline( inFile, Line ) )            // While there is still a line, add up number of rows
    {
        Rows++;
    }

    return Rows;
}

void readData( istream &inFile )
{
    string line;
    size_t position = 0;

    while ( getline( inFile, line ) )
    {
        cout << line << "\t";
        position = line.find_first_of( "/ :," );

        while ( position != string::npos )
        {
            cout << line[position];
            position = line.find_first_of( "/ :,", position + 1 );
        }

        cout << endl;
    }
}

Problem 1: initializing the array "data" with rows as arrayRows gives me an error. If I change arrayRows to a number, the code works and shows that findRows does return a value to arrayRows. So what's the problem?

Problem 2: None of the cout's in readData actually cout anything. However, the code does run. The only thing that displays is
cout << "Number of rows of data: " << arrayRows
If I move all the code to main, it works, so the problem must have something to do with passing the ifstream object by reference. How can i fix this?

Thanks :) :)

Hi again,

After doing what you said, I bumped into another problem.

It's not nice to bump on a forum :D

You cannot create an array nor matrix using variables for its size.

But I initialized the "variable" as a const int.... :'(

Any idea about the second prob? :)

1. Probably you have never seen that... const var with constant-expression initializator.

2. Your inData stream is in eof state after findRows call. It can't get you anything in readData. Do close then reopen or reinitialize the stream:

inData.clear();
inData.seekg(0,ios_base::beg);

Now you may bump into another Great C++ Problem like these ;)

This article has been dead for over six months. Start a new discussion instead.