Thread: ifstream and istream issue

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    ifstream and istream issue

    Hello,
    I found following source code:

    Code:
    void func_read(istream&, vector<string>&);
    void func_read_file(ifstream&, vector<string>);
    
    //and implementations
    
    void func_read(istream& in, vector<string>& v)
    {
    	string s;
    	while( in >> s )
    	{
    		v.push_back(s);
    	}
    }
    
    void func_read_file(ifstream& in, vector<string> v)
    {
    	string s;
    	while( in >> s )
    	{
    		v.push_back(s);
    	}
    }
    I really don't know why this is written like this, I tested func_read:

    Code:
    int main()
    {
    	vector<string> v;
    
    	ifstream in("test.txt");
    
    	func_read( in, v );
    	
    	vector<string>::const_iterator it;
    
    	for(it = v.begin(); it != v.end(); ++it)
    		cout<<*it<<endl;
    
    }
    and everything seem to be OK, and it works fine since istream can accept referece to ifstream. So I assume there is no need to write special function to read from file (ifstream&).

    Can you confirm this?
    Is there any situations where this approach is necessary?

    Thanks very much
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    and everything seem to be OK, and it works fine since istream can accept referece to ifstream. So I assume there is no need to write special function to read from file (ifstream&).
    Yes, using the istream version, func_read(), is sufficient. func_read_file() is broken anyway, since the vector is passed by value.

    Is there any situations where this approach is necessary?
    When you want more flexibility, e.g. option of using cin or an istringstream instead of an ifstream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to pass an ifstream object to a function
    By chintugavali in forum C++ Programming
    Replies: 14
    Last Post: 12-18-2007, 09:58 PM
  2. istream
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2007, 05:11 PM
  3. Converting an ifstream to an istream
    By manannan in forum C++ Programming
    Replies: 10
    Last Post: 08-31-2005, 06:26 AM
  4. Returning failure result for operator << istream
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 07-05-2003, 05:23 PM
  5. Replies: 5
    Last Post: 11-24-2002, 11:05 PM