Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem | Istream_iterator in std::copy

Reply
Thread Tools

Problem | Istream_iterator in std::copy

 
 
Pradeep
Guest
Posts: n/a
 
      10-19-2006
Hi All,

I am facing some problem using istream_iterator for reading the
contents of a file and copying it in a vector of strings.However the
same thing works for a vector of integers.

The code that doesn't work is

std::vector<std::string> vecStr;
std::ifstream fstrRead("Test.txt");
if(!fstrRead)
{
std::cerr<<"Cannot Read the file"<<std::endl;
exit(-1);
}

std::copy(std::istream_iterator<std::string>(fstrR ead),
std::istream_iterator<std::string> (),
std::back_inserter(vecStr));


The problem is in std::istream_iterator<std::string> (). The
constructor calls the method
void _Getval()
{ // get a _Ty value if possible
if (_Myistr != 0 && !(*_Myistr >> _Myval))
_Myistr = 0;
}

where it fails in conversion.
The compiler error is c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\iterator(213): error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'std::string' (or there
is no acceptable conversion)


However this works fine if it is an vector of integers.

vector<int> vi;//vector to be filled
ifstream vi_dump("vi.txt"); //open for read
if (!vi_dump)
{
cerr<<"couldn't open file";
exit(1);
}
copy(istream_iterator<int> (vi_dump),
istream_iterator<int> (),
back_inserter(vi));

I would like to know the reason for this and how it can be solved?

Thanks in Advance,
Pradeep

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      10-19-2006
Pradeep wrote:
> Hi All,
>
> I am facing some problem using istream_iterator for reading the
> contents of a file and copying it in a vector of strings.However the
> same thing works for a vector of integers.
>
> The code that doesn't work is
>
> std::vector<std::string> vecStr;
> std::ifstream fstrRead("Test.txt");
> if(!fstrRead)
> {
> std::cerr<<"Cannot Read the file"<<std::endl;
> exit(-1);
> }
>
> std::copy(std::istream_iterator<std::string>(fstrR ead),
> std::istream_iterator<std::string> (),
> std::back_inserter(vecStr));



Well you'll need to post the actual program(a minimal version that
demonstrates your problem). Please see:
http://www.parashift.com/c++-faq-lite/how-to-post.html


Try compiling:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>


int main()
{
std::vector<std::string> vecStr;
std::ifstream fstrRead("Test.txt");
if(!fstrRead)
{
std::cerr<<"Cannot Read the file"<<std::endl;
exit(-1);
}

std::copy(std::istream_iterator<std::string>(fstrR ead),
std::istream_iterator<std::string> (),
std::back_inserter(vecStr));
}


It compiles for me on both VC++ and Comeau. I suspect that it a case of
your missing out on an #include.

Regards,
Sumit.
 
Reply With Quote
 
 
 
 
Piyush
Guest
Posts: n/a
 
      10-19-2006

Pradeep wrote:

> Hi All,
>
> I am facing some problem using istream_iterator for reading the
> contents of a file and copying it in a vector of strings.However the
> same thing works for a vector of integers.
>
> The code that doesn't work is
>
> std::vector<std::string> vecStr;
> std::ifstream fstrRead("Test.txt");
> if(!fstrRead)
> {
> std::cerr<<"Cannot Read the file"<<std::endl;
> exit(-1);
> }
>
> std::copy(std::istream_iterator<std::string>(fstrR ead),
> std::istream_iterator<std::string> (),
> std::back_inserter(vecStr));
>
>
> The problem is in std::istream_iterator<std::string> (). The
> constructor calls the method
> void _Getval()
> { // get a _Ty value if possible
> if (_Myistr != 0 && !(*_Myistr >> _Myval))
> _Myistr = 0;
> }
>
> where it fails in conversion.
> The compiler error is c:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\include\iterator(213): error C2679: binary '>>' : no operator
> found which takes a right-hand operand of type 'std::string' (or there
> is no acceptable conversion)
>
>
> However this works fine if it is an vector of integers.
>
> vector<int> vi;//vector to be filled
> ifstream vi_dump("vi.txt"); //open for read
> if (!vi_dump)
> {
> cerr<<"couldn't open file";
> exit(1);
> }
> copy(istream_iterator<int> (vi_dump),
> istream_iterator<int> (),
> back_inserter(vi));
>
> I would like to know the reason for this and how it can be solved?
>
> Thanks in Advance,
> Pradeep


Hey,
The problem with your code is that the string class has a friend
function which handles reading and writing it from a stream.(the >> and
<< operators). Now since this function is declared in the string header
once you include this header the code compiles.
Regards,
Piyush

 
Reply With Quote
 
Pradeep
Guest
Posts: n/a
 
      10-19-2006
Thanks guys. It was a silly mistake.

However the problem with this is that this gives a vector of all words
but I want a vector of lines.

e.g. line 1 "I have a problem"
line 2 "got it"

Now this gives me a vector of strings with size 6 and all the words.
However I want a vector with size 2 and each line in one string.

Any idea around that.

Thanks
Pradeep


Piyush wrote:
> Pradeep wrote:
>
> > Hi All,
> >
> > I am facing some problem using istream_iterator for reading the
> > contents of a file and copying it in a vector of strings.However the
> > same thing works for a vector of integers.
> >
> > The code that doesn't work is
> >
> > std::vector<std::string> vecStr;
> > std::ifstream fstrRead("Test.txt");
> > if(!fstrRead)
> > {
> > std::cerr<<"Cannot Read the file"<<std::endl;
> > exit(-1);
> > }
> >
> > std::copy(std::istream_iterator<std::string>(fstrR ead),
> > std::istream_iterator<std::string> (),
> > std::back_inserter(vecStr));
> >
> >
> > The problem is in std::istream_iterator<std::string> (). The
> > constructor calls the method
> > void _Getval()
> > { // get a _Ty value if possible
> > if (_Myistr != 0 && !(*_Myistr >> _Myval))
> > _Myistr = 0;
> > }
> >
> > where it fails in conversion.
> > The compiler error is c:\Program Files\Microsoft Visual Studio .NET
> > 2003\Vc7\include\iterator(213): error C2679: binary '>>' : no operator
> > found which takes a right-hand operand of type 'std::string' (or there
> > is no acceptable conversion)
> >
> >
> > However this works fine if it is an vector of integers.
> >
> > vector<int> vi;//vector to be filled
> > ifstream vi_dump("vi.txt"); //open for read
> > if (!vi_dump)
> > {
> > cerr<<"couldn't open file";
> > exit(1);
> > }
> > copy(istream_iterator<int> (vi_dump),
> > istream_iterator<int> (),
> > back_inserter(vi));
> >
> > I would like to know the reason for this and how it can be solved?
> >
> > Thanks in Advance,
> > Pradeep

>
> Hey,
> The problem with your code is that the string class has a friend
> function which handles reading and writing it from a stream.(the >> and
> << operators). Now since this function is declared in the string header
> once you include this header the code compiles.
> Regards,
> Piyush


 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      10-19-2006
Pradeep <(E-Mail Removed)> wrote:
> Thanks guys. It was a silly mistake.
>
> However the problem with this is that this gives a vector of all words
> but I want a vector of lines.
>
> e.g. line 1 "I have a problem"
> line 2 "got it"
>
> Now this gives me a vector of strings with size 6 and all the words.
> However I want a vector with size 2 and each line in one string.
>
> Any idea around that.


Hi Pradeep,
First, in this newsgroup it is common etiquette not to top-post. Your
responses belong either below, or interspersed with, *properly trimmed*
quotes.

To address your question, please see this thread:

http://groups.google.com/group/comp....ca1ef940f093d/

(look at post 6 by Jerry Coffin).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
=?ISO-8859-1?Q?Jens_M=FCller?=
Guest
Posts: n/a
 
      10-19-2006
Sumit Rajan wrote:

>
> Try compiling:
>
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <vector>
>


> std::cerr<<"Cannot Read the file"<<std::endl;


Isn't std::endl necessarily defined only in <ostream>?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem about istream_iterator tong C++ 2 01-11-2007 12:05 AM
Problem with std::istream_iterator dragoncoder C++ 8 09-18-2006 10:27 PM
istream_iterator & copying files Alex Vinokur C++ 10 04-20-2004 07:16 PM
istream_iterator and ostream_iterator problem Chris Mantoulidis C++ 2 12-15-2003 12:14 PM
istream_iterator question Bill Rudolph C++ 4 08-15-2003 07:28 AM



Advertisments