C++ Function takes File Object as Reference Parameter

This is a discussion on C++ Function takes File Object as Reference Parameter within the C++ Programming forums, part of the General Programming Boards category; I have a school project in which need to create a function that takes a File Object as a Reference ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    3

    C++ Function takes File Object as Reference Parameter

    I have a school project in which need to create a function that takes a File Object as a Reference Parameter. Supposedly, it should allow me to read the first piece of data from others separated by a space from a file. The later be able to continue reading from the next piece of data.

    I know how to set things up to read from the data file, such as using
    Code:
    #include <iostream>
    , and
    Code:
    ifstream inputFileName;
    inputFile.open ("nameOfFile");
    I also understand how to pass a variable by reference to a function. But I simply cannot put the two items together!

    I tried using
    Code:
    void getFileInput (ifstream &); //parameter
    getFileInput ("nameOfFile")  // call
    void getFileInput (ifstream &dataFileName)
    In the function I used
    Code:
    ifstream inputFile;    
    inputFile.open (dataFileName);
    I'm just not getting an understanding of this concept! If you could help understand I would be quite appreciative!!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You defined your function to take an ifstream reference, why are you trying to pass a string instead? Wouldn't it make more sense to send your ifstream instance?
    Code:
    getFileInput (inputFileName);

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Thanks Jim,
    I would like to send the ifstream instance but I don't know how..either in the call or in the function definition.
    This is more of what i have:
    Code:
    #include <iostream>  
    #include <cstdlib>  
    #include <fstream>  
    
    
    using namespace std;  
     
    void getFileInput (ifstream &);
    
    
    int main( )  
    {  
    
    
        string fileName;
        fileName = "test.txt";    
    
    
        getFileInput ( inputFileName );
    
    
        system("PAUSE");              
        return 0;     
     }  
    
    
    
    
    void getFileName (ifstream &dataFileName)
    { 
        ifstream testFileName;
        testFileName.open( dataFileName );
        string variable1;
        testFileName >> variable1;
    }
    And I am lost!!!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    In order to pass a ifstream instance to your function you need to declare an instance of an ifstream, then pass that instance to the function. You won't create the instance in your function you will use the instance you passed.

    Code:
    #include <iostream> 
    #include <fstream> 
    #include <string>
      
    using namespace std; 
      
    void getFileInput (ifstream &);
      
    int main( ) 
    { 
        string fileName;
        fileName = "test.txt";   
     
        // Create an ifstream instance and open the fileName file for processing.
        ifstream inStream(fileName);
        // Pass this stream into the function.
        getFileInput ( inStream );
     
        return 0;    
     } 
    
    void getFileInput (ifstream &fileIn)
    {
       string someString;
       // Use the stream in this function, for example read a string.
       fileIn >> someString;
    
    }
    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    3
    Thanks All! I got it figured out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function accepting reference as parameter?
    By rodrigorules in forum C++ Programming
    Replies: 5
    Last Post: 11-23-2009, 03:58 AM
  2. Replies: 4
    Last Post: 01-23-2008, 05:21 AM
  3. A pointer to an object's function as a parameter
    By wiiire in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2007, 10:38 PM
  4. passing an object as an argument to a function (by reference)
    By odysseus.lost in forum C++ Programming
    Replies: 5
    Last Post: 12-15-2005, 11:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21