 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pradeep Guest
|
Posted: Thu Oct 19, 2006 9:10 am Post subject: Problem | Istream_iterator in std::copy |
|
|
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>(fstrRead),
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 |
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Thu Oct 19, 2006 9:10 am Post subject: Re: Problem | Istream_iterator in std::copy |
|
|
Pradeep wrote:
| Quote: | 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>(fstrRead),
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>(fstrRead),
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. |
|
| Back to top |
|
 |
Piyush Guest
|
Posted: Thu Oct 19, 2006 9:10 am Post subject: Re: Problem | Istream_iterator in std::copy |
|
|
Pradeep wrote:
| Quote: | 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>(fstrRead),
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 |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|