passing an ifstream variable

This is a discussion on passing an ifstream variable within the C++ Programming forums, part of the General Programming Boards category; OK this is probably really simple, but for some reason when I pass through the ifstream variable inStream, the counter ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    passing an ifstream variable

    OK this is probably really simple, but for some reason when I pass through the ifstream variable inStream, the counter doesn't work...

    when the counter is included in the OpenFile function it works fine...but when I place it in its proper function, and pass through inStream it doesn't. BTW elements is a private member of the class,

    Code:
    //FILE NAME: readfile.cpp
    
    /*
     * definitions for class ReadFile
     */
    
    //=====================================
    ReadFile::ReadFile()
    {
    	elements = 0;
    }
    
    
    //=====================================
    void ReadFile::openFile( string fileName )
    {
    	ifstream inStream;	
    
    	inStream.open( fileName.c_str() ); //open user chosen file
    
    	//check if filed open correctly, if not
    	//show error message, and terminate
    	if ( inStream.fail() ) {	
    		cout << "Input file opening for " << fileName << " failed.  Exiting...\n\n";
    		exit(-1);
    	}
      
    	countElements( inStream );
    	
    	inStream.close();
    }
    
    //=====================================
    void ReadFile::countElements( ifstream inStream )
    {
    	string temp;	//dummy var to keep the line
    
    	//start counting elements procedure
    	if(inStream.good())
    	{  
    		while(!inStream.eof() && inStream.good())
    		{		
    			getline(inStream,temp);
    			elements++;
    		}
    	}
    	
    	/*****************************************
    	ERROR CHECKING - DELETE
    	******************************************/
    	cout << "There are " << elements << " elements." << endl;
    	
    	inStream.close(); //close file
    }
    this is the class definition
    Code:
    //FILE NAME: readfile.h
    
    /*
     * File that reads in the file:
     * -the first pass, counts the number of elements in the file
     * -the second pass puts the file into the map
     * -the third pass makes the adjecency list
     */
    
    class ReadFile
    {
    public:	
    	ReadFile();
    
    	//function declarations
    	void openFile( string fileName );	
    	void countElements( ifstream inFile );
    	void readIntoMap();
    	void readIntoList();
    
    private:
    	int elements; //number of elements
    };
    
    #include "readFile.cpp"

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    nevermind....stupid VC++!

    hehe, actually it was a vc++ error, for some reason it wasn't compiling that file....go figure....it works now.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Line Variable Passing (like in DOS)
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2002, 07:31 AM
  2. Passing the variable as a Reference
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 01:35 PM
  3. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 11:42 AM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 02:02 PM
  5. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 10:43 PM

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