473,557 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::istreambuf _iterator

Hi,

This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?

I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf _iterator as follows (where inputFile is an ifstream):

std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())

The problem I am facing is that I cannot then iterate over vector of
strings in the usual manner;

std::vector<std ::string>::iter ator(data.begin ());
OR
std::vector<std ::string>::iter ator(data->begin());

I keep getting compilation errors:

error: request for member `begin' in `data', which is of non-aggregate
type `std::vector<st d::string, std::allocator< std::string ()
(std::istreambu f_iterator<std: :string, std::char_trait s<std::string>
>, std::istreambuf _iterator<std:: string, std::char_trait s<std::string>
(*)())'
I must be missing something completely. I'd greatly appreciate some
help. Thanks.

Aug 9 '07 #1
9 4422

ahmadcorp <ma*****@gmail. comwrote in message...
Hi,
This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?

I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf _iterator as follows (where inputFile is an ifstream):

std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())

The problem I am facing is that I cannot then iterate over vector of
strings in the usual manner;

std::vector<std ::string>::iter ator(data.begin ());
OR
std::vector<std ::string>::iter ator(data->begin());

I keep getting compilation errors:

error: request for member `begin' in `data', which is of non-aggregate
type `std::vector<st d::string, std::allocator< std::string ()
(std::istreambu f_iterator<std: :string, std::char_trait s<std::string>
, std::istreambuf _iterator<std:: string, std::char_trait s<std::string>
(*)())'

I must be missing something completely. I'd greatly appreciate some
help. Thanks.
I prefer:

std::vector<std ::stringdata;
for( std::string line; std::getline( inputFile, line ); /*m t*/ ){
data.push_back( line );
}

Try this (note the '.rdbuf()' addition):

std::vector<std ::stringdata(
std::istreambuf _iterator<std:: string>( inputFile.rdbuf () ),
std::istreambuf _iterator<std:: string>()
);

Does it compile now?

--
Bob R
POVrookie
Aug 9 '07 #2

ahmadcorp <ma*****@gmail. comwrote in message...
Hi,

This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?

I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf _iterator as follows (where inputFile is an ifstream):

std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())

The problem I am facing is that I cannot then iterate over vector of
strings in the usual manner;

std::vector<std ::string>::iter ator(data.begin ());
OR
std::vector<std ::string>::iter ator(data->begin());

I keep getting compilation errors:

error: request for member `begin' in `data', which is of non-aggregate
type `std::vector<st d::string, std::allocator< std::string ()
(std::istreambu f_iterator<std: :string, std::char_trait s<std::string>
, std::istreambuf _iterator<std:: string, std::char_trait s<std::string>
(*)())'

I must be missing something completely. I'd greatly appreciate some
help. Thanks.
I prefer:

std::vector<std ::stringdata;

// ------- lines
for( std::string line; std::getline( inputFile, line ); /*m t*/ ){
data.push_back( line );
}
// -------

// ------- words
std::copy(
std::istream_it erator<std::str ing>( inputFile ),
std::istream_it erator<std::str ing>(),
std::back_inser ter( data ) );
// -------

std::copy( data.begin(), data.end(),
std::ostream_it erator<std::str ing>( std::cout, "\n" ) );

[from my (old) docs]
// -------
The istream and ostream classes are meant to handle conversion between
objects in your program and their textual representation.

By contrast, the underlying streambuf class is for transferring raw bytes
between your program, and input sources or output sinks. Different streambuf
subclasses connect to different kinds of sources and sinks.
// -------

std::vector<uns igned charImage( // or <char>
std::istreambuf _iterator<char> ( inputFile.rdbuf () ),
std::istreambuf _iterator<char> ()
);

--
Bob R
POVrookie
Aug 9 '07 #3
On Aug 9, 6:50 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
ahmadcorp <manz...@gmail. comwrote in message...
Hi,
This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?
I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf _iterator as follows (where inputFile is an ifstream):
std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())
The problem I am facing is that I cannot then iterate over vector of
strings in the usual manner;
std::vector<std ::string>::iter ator(data.begin ());
OR
std::vector<std ::string>::iter ator(data->begin());
I keep getting compilation errors:
error: request for member `begin' in `data', which is of non-aggregate
type `std::vector<st d::string, std::allocator< std::string ()
(std::istreambu f_iterator<std: :string, std::char_trait s<std::string>
>, std::istreambuf _iterator<std:: string, std::char_trait s<std::string>
(*)())'
I must be missing something completely. I'd greatly appreciate some
help. Thanks.

I prefer:

std::vector<std ::stringdata;

// ------- lines
for( std::string line; std::getline( inputFile, line ); /*m t*/ ){
data.push_back( line );
}
// -------

// ------- words
std::copy(
std::istream_it erator<std::str ing>( inputFile ),
std::istream_it erator<std::str ing>(),
std::back_inser ter( data ) );
// -------

std::copy( data.begin(), data.end(),
std::ostream_it erator<std::str ing>( std::cout, "\n" ) );

[from my (old) docs]
// -------
The istream and ostream classes are meant to handle conversion between
objects in your program and their textual representation.

By contrast, the underlying streambuf class is for transferring raw bytes
between your program, and input sources or output sinks. Different streambuf
subclasses connect to different kinds of sources and sinks.
// -------

std::vector<uns igned charImage( // or <char>
std::istreambuf _iterator<char> ( inputFile.rdbuf () ),
std::istreambuf _iterator<char> ()
);

--
Bob R
POVrookie

Hi,

Thanks for the reply. Works like a charm.

Decided to memory map the file and then process internally. Seems to
be a lot faster than reading file line by line.

Maz.

Aug 10 '07 #4
Hi!

ahmadcorp schrieb:
std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())
[snip]
error: request for member `begin' in `data', which is of non-aggregate
type `std::vector<st d::string, std::allocator< std::string ()
(std::istreambu f_iterator<std: :string, std::char_trait s<std::string>
>, std::istreambuf _iterator<std:: string, std::char_trait s<std::string>
(*)())'
The "data" variable is not a variable at all. That is because the
declaration of "data" can be interpreted as a function declaration. The
compiler is required to prefer that. Thus "data" is a function. And
functions don't have "begin()" and "end()".

It can be solved by having extra variables:

void foo(std::istrea m& file)
{
using namespace std;
istream_iterato r<stringfirst(f ile), last;
vector<stringda ta(first, last);

data.begin(); //works
}
HTH, Frank
Aug 10 '07 #5
On Aug 9, 1:57 pm, ahmadcorp <manz...@gmail. comwrote:
This should be a simple problem, but I can't seem to figure it out.
Maybe someone can help?
It's a very simple problem, once you've seen it. On the other
hand, I don't know of anyone who hasn't hit it once or twice.
I have a file containing data each line has about 6 entries (numbers)
space delimited and each line has a new line character at the end of
the line. I'd like to read the file in one pass, so I am trying to use
std::istreambuf _iterator as follows (where inputFile is an ifstream):
std::vector<std ::string>
data( std::istreambuf _iterator<std:: string>(inputFi le) ,
std::istreambuf _iterator<std:: string>())
Attention: here, you are not defining a variable, but declaring
a function. (Isn't C++ declaration syntax horrible.) You need
to do something so that at least one of the "arguments" can't be
interpreted as a parameter declaration; an extra pair of
parentheses will do the trick:

std::vector< std::string >
data(
(std::istreambu f_iterator< std::string >( inputFile )),
(std::istreambu f_iterator< std::string >()) ) ;

(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)

--
James Kanze (GABI Software) email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 11 '07 #6

James Kanze <ja*********@gm ail.comwrote in message...

/* """ quote
Attention: here, you are not defining a variable, but declaring
a function. (Isn't C++ declaration syntax horrible.) You need
to do something so that at least one of the "arguments" can't be
interpreted as a parameter declaration; an extra pair of
parentheses will do the trick:

std::vector< std::string >
data(
(std::istreambu f_iterator< std::string >( inputFile )),
(std::istreambu f_iterator< std::string >()) ) ;

(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)
""" */

Did that compile for you ( using std::string in istreambuf_iter ator!! )?
(which compiler?)

--
Bob R
POVrookie
Aug 11 '07 #7
On Aug 11, 6:16 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
James Kanze <james.ka...@gm ail.comwrote in message...
Attention: here, you are not defining a variable, but declaring
a function. (Isn't C++ declaration syntax horrible.) You need
to do something so that at least one of the "arguments" can't be
interpreted as a parameter declaration; an extra pair of
parentheses will do the trick:
std::vector< std::string >
data(
(std::istreambu f_iterator< std::string >( inputFile )),
(std::istreambu f_iterator< std::string >()) ) ;
(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)
Did that compile for you ( using std::string in
istreambuf_iter ator!! )? (which compiler?)
You're right, I didn't try it:-). You'd need
std::istream_it erator for std::string. Don't know what I was
thinking of.

--
James Kanze (GABI Software) email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 11 '07 #8

James Kanze <ja*********@gm ail.comwrote in message...

/* """
On Aug 11, 6:16 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
James Kanze <james.ka...@gm ail.comwrote in message...
std::vector< std::string data(
(std::istreambu f_iterator< std::string >( inputFile )),
(std::istreambu f_iterator< std::string >()) ) ;
(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)
Did that compile for you ( using std::string in
istreambuf_iter ator!! )? (which compiler?)
You're right, I didn't try it:-). You'd need
std::istream_it erator for std::string.
""" */
Don't know what I was thinking of.
Probably that thread a while back. But, that was using type 'char'.

// ref: std::ifstream PicIn( ...., '::in | '::binary );

std::vector<uns igned charImage(
std::istreambuf _iterator<char> ( PicIn.rdbuf() ),
std::istreambuf _iterator<char> ()
);

The code you showed was the *first* thing I tried. <G>
[ so, my 'single-brain-cell' brain *does* retain some things. :-} ]

--
Bob R
POVrookie
Aug 11 '07 #9
On Aug 11, 11:51 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
James Kanze <james.ka...@gm ail.comwrote in message...
On Aug 11, 6:16 pm, "BobR" <removeBadB...@ worldnet.att.ne twrote:
James Kanze <james.ka...@gm ail.comwrote in message...
std::vector< std::string data(
(std::istreambu f_iterator< std::string >( inputFile )),
(std::istreambu f_iterator< std::string >()) ) ;
(Formally, it's sufficient to add the parentheses to just one of
the arguments, but I like orthogonality.)
Did that compile for you ( using std::string in
istreambuf_iter ator!! )? (which compiler?)
You're right, I didn't try it:-). You'd need
std::istream_it erator for std::string.
Don't know what I was thinking of.
Probably that thread a while back. But, that was using type 'char'.
Not really. I was basing my response on the original posting,
plus the error message he got. The error message was due to his
declaring a function, not a variable. (And of course, declaring
std::istreambuf _iterator< std::string as a parameter of a
function probably won't cause any errors from the compiler,
since using a class template as a function parameter in a
declaration doesn't trigger instantiation.)
// ref: std::ifstream PicIn( ...., '::in | '::binary );
std::vector<uns igned charImage(
std::istreambuf _iterator<char> ( PicIn.rdbuf() ),
std::istreambuf _iterator<char> ()
);
The code you showed was the *first* thing I tried. <G>
Which, of course, would generate a different error message than
the one he got. So he really had two problems. The data
definition which is really a function declaration is so common
that I didn't think further, but that's not really an excuse for
posting an answer without having actually tried it. (Formally,
std::istreambuf _iterator< std::string is undefined behavior,
so you're not guaranteed a compiler error. But I'd be very
surprised if you didn't get one if you actually tried to use it.

--
James Kanze (GABI Software) email:james.ka. ..@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 12 '07 #10

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

Similar topics

1
3045
by: Dave Townsend | last post by:
Hi, Can anybody help me with the following piece of code? The purpose behind the code is to parse HTML files, strip out the tags and return the text between tags. This is part of a larger application which will perform "searches" for text values in a directory of html files, trying to match only the non-tagged text in the documents.
103
48551
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things,...
12
11636
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include...
6
6595
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
6
3227
by: ma740988 | last post by:
I've been perusing for some time now - I suppose some of the algorithms in Josuttis. 'Today', I do alot of funny math on data resident within memory. As part of this. I find myself using std::search_n alot to find a particular value in a range of memory. I'm fairly pleased with std::search_n - in that my benchmark ( below ) shows on...
4
2309
by: mathieu | last post by:
Hello, I am looking at the API of std::vector but I cannot find a way to specify explicitely the size of my std::vector. I would like to avoid vector::resize since it first initializes the elements of the vector. Thank you ! Mathieu Code:
2
2072
by: arnuld | last post by:
I have 2 programs. 1st PROGRAM: copies an input file to standard output but it does so at the expense of deleting all white-space (space, tabs, newlinies etc). 2nd PROGRAM: copies the input as it is to the standard output.
7
5091
by: Sanyi | last post by:
What is cin's streambuffer's state and content after the following (using namespace std)? string((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>()) ); I am trying to use this in a loop (to get a 'multiple cat'-like behavior) and the second time around it returns an empty string instead of waiting for more input. This makes me...
0
1468
by: mathieu | last post by:
Hi there, I think I misunderstanded the use of istreambuf_iterator. I thought I could use it this way (*). Which would allow me to skip the call to vector::resize(). Could someone please let me know what is wrong in my code. My goal is simply to skip the default initialization to 0 of the std::vector which is compulsary for 'DoMethod1'. ...
0
7626
marktang
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...
0
7830
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7911
tracyyun
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...
0
6180
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5172
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...
0
3577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2044
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
1
1161
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
867
bsmnconsultancy
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...

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.