ifstream and istream issue

This is a discussion on ifstream and istream issue within the C++ Programming forums, part of the General Programming Boards category; 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) ...

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

    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08: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, 10:05 PM

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