Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

istream_iterator problem? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
This must be a problem with my crappy Visual C++ 6.0 compiler, but I thought I'd check if I'm doing something wrong...

The code below is almost identical to Item 6 of "Effective STL" (I'm just using a string instead of list), but it gives me a C2664 error on the last line.

If it's a compiler problem, does anyone know a workaround in VC++ 6.0?
Code:
	ifstream input( "file.txt" );

	if ( !input )
	{
		cout << "Error opening file!" << endl;
		return 1;
	}

	istream_iterator<char> start( input );
	istream_iterator<char> end;

	string strErr( start, end );  // error C2664!
 
With the right #includes it works for me in VC 6.0:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>

int main()
{
    using namespace std;

    ifstream input( "file.txt" );

    if ( !input )
    {
        cout << "Error opening file!" << endl;
        return 1;
    }

    istream_iterator<char> start( input );
    istream_iterator<char> end;

    string strErr( start, end );  // error C2664!

    return 0;
}
 
That's really strange. I even copy & pasted your exact code (which wasn't much different than mine) and still get this error:
Code:
error C2664: '__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(const char *,unsigned int,const class std::allocator<char> &)' : cannot convert parameter 1 from 'class std::istream_iterator<char,char,struct std::char_traits<char> >' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
 
Thanks, I had a feeling it wasn't a problem with the code.
I'm downloading VC 2005 and gcc now and I'll see how those work.
That page says it was fixed in VC 6.0... Yeah right! :D
 
I just installed VC 2005 and it compiles fine. :)
One strange thing I noticed when running it is that all the whitespace is stripped off in the string. I'm guessing that's because it's using the default operator>> for string...
Is there a way to keep the whitespace without having to read the file into the string the long way?
 
Nevermind, I found it. I need to use istreambuf_iterator instead of istream_iterator.
Code:
istreambuf_iterator<char> start( input );
istreambuf_iterator<char> end;
string strFile( start, end );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top