473,769 Members | 2,214 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 4443

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
3056
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
48753
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, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
12
11665
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 <iomanip> #include <fstream> #include <iostream>
6
6645
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
3238
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 average an 8 second performance gain comparable to a for loop. The question. Since I haven't quite...
4
2320
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
2083
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
5106
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 think that either the end-of-stream stays in the buffer or cin or its buffer is in some bad
0
1490
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'. At least on my OS: Debian & g++-4.2 -03, DoMethod2 is slower by a factor of 3.
0
10206
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
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...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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();...
0
5293
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...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
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 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.