473,226 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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::stringvecStr;
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<intvi;//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

Oct 19 '06 #1
5 2787
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::stringvecStr;
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::stringvecStr;
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.
Oct 19 '06 #2

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::stringvecStr;
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<intvi;//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

Oct 19 '06 #3
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::stringvecStr;
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<intvi;//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
Oct 19 '06 #4
Pradeep <pr***********@gmail.comwrote:
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
Oct 19 '06 #5
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>?
Oct 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
4
by: Simon Elliott | last post by:
Is there an equivalent of std::copy which works on STL containers for overlapping ranges? -- Simon Elliott http://www.ctsn.co.uk
3
by: kathy | last post by:
I have array: double a; double b; std::vector <double> vDouble; when I use: std::copy(a,a+1024,vDouble.begin());
1
by: Siegfried Heintze | last post by:
What is the minimum I must type to create a custom iterator that will allow me display my iterator on std::cout using std::copy? Thanks, Siegfried
1
by: Siegfried Heintze | last post by:
Should the following work? It does on some compilers. How can I get it to work on g++ 3.2? On g++ 3.2 it keeps telling me that there is no such function operator << for ostream and...
6
by: Chris Johnson | last post by:
Greetings all. I am really stuck on this one as I can't seem to grok if I am abusing the C++ language or if I am simply using components of the C++ Standard Library incorrectly. Here is the code:...
3
by: none | last post by:
Hi, Consider the following piece of code: int t={1,2,3,4,5,6}; vector<intv; std::copy (t, t+sizeof(t)/sizeof(t), std::back_inserter (v)); Could someone explain me why we can pass "t" as an...
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.