Hi All,
I am facing some problem using istream_iterato r 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<<"Can not Read the file"<<std::end l;
exit(-1);
}
std::copy(std:: istream_iterato r<std::string>( fstrRead),
std::istream_it erator<std::str ing(),
std::back_inser ter(vecStr));
The problem is in std::istream_it erator<std::str ing(). 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\includ e\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_it erator<int(vi_d ump),
istream_iterato r<int(),
back_inserter(v i));
I would like to know the reason for this and how it can be solved?
Thanks in Advance,
Pradeep 5 2834
Pradeep wrote:
Hi All,
I am facing some problem using istream_iterato r 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<<"Can not Read the file"<<std::end l;
exit(-1);
}
std::copy(std:: istream_iterato r<std::string>( fstrRead),
std::istream_it erator<std::str ing(),
std::back_inser ter(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<<"Can not Read the file"<<std::end l;
exit(-1);
}
std::copy(std:: istream_iterato r<std::string>( fstrRead),
std::istream_it erator<std::str ing(),
std::back_inser ter(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.
Pradeep wrote:
Hi All,
I am facing some problem using istream_iterato r 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<<"Can not Read the file"<<std::end l;
exit(-1);
}
std::copy(std:: istream_iterato r<std::string>( fstrRead),
std::istream_it erator<std::str ing(),
std::back_inser ter(vecStr));
The problem is in std::istream_it erator<std::str ing(). 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\includ e\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_it erator<int(vi_d ump),
istream_iterato r<int(),
back_inserter(v i));
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
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_iterato r 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<<"Can not Read the file"<<std::end l;
exit(-1);
}
std::copy(std:: istream_iterato r<std::string>( fstrRead),
std::istream_it erator<std::str ing(),
std::back_inser ter(vecStr));
The problem is in std::istream_it erator<std::str ing(). 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\includ e\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_it erator<int(vi_d ump),
istream_iterato r<int(),
back_inserter(v i));
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
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
Sumit Rajan wrote:
>
Try compiling:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
std::cerr<<"Can not Read the file"<<std::end l;
Isn't std::endl necessarily defined only in <ostream>? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 it seems to ignore '\n'
ifstream in("somefile");
ofstream out("someOtherFile");
std::copy(std::istream_iterator<unsigned char>(in),
|
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 this?
I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it ... but some tests seem to indicate
that my pointer copy...
|
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
|
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());
|
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
| |
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 pair<string,string>. But there is!
And I can call it! And it works! So why cannot std::copy find it?
thanks,
Siegfried
template<typename K, typename V>
|
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:
#include <string>
#include <map>
#include <iostream>
#include <utility>
#include <iterator>
|
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 argument to the copy
method though it is expecting an iterator as its firts argument like
written in the STL doc:
|
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 efficient, so I used reference to vector.
2) anything else you think worth mentioning
/* A program that asks the user for input and when user hits EOF will sort the words
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |