473,397 Members | 1,961 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,397 software developers and data experts.

COPYING (not extracting) data from an istream object

Is there any way to do this? I've tried tellg() followed by seekg(),
inserting the stream buffer to an ostringstream (ala os << is.rdbuf()),
read(), and having no luck.

The problem is, all of these methods EXTRACT the data at one
point or another. The other problem is there appears to be NO
WAY to get at the actual buffer pointer (char*) of the characters
in the stream. There is a way to get the streambuf object
associated with the stream (rdbuf()), but the gptr() member
function of the streambuf object is protected.

Help!!!

--Randy Yates

Jan 16 '06 #1
13 3708
>The problem is, all of these methods EXTRACT the data at one
point or another. The other problem is there appears to be NO
WAY to get at the actual buffer pointer (char*) of the characters
in the stream. There is a way to get the streambuf object
associated with the stream (rdbuf()), but the gptr() member
function of the streambuf object is protected.


How many streams can you have at one time? If I open a file ifstream
fin1("blah.txt") and ifstream fin2("blah.txt"), then are fin1 and fin2
copies of each other or are they both referencing the same stream?

Another question comes to mind: you took an output stream, and copied
it. If you flush both streams, lets say at one time in a (OT) thread
(/OT) is the behavior defined somewhere?

Jan 16 '06 #2
Shark,

I don't understand the point of your first question, and I don't
understand your second question at all. The answer to the
first question is "one." I will only have one stream open at a
time in this context.

--Randy

Jan 16 '06 #3
>I don't understand the point of your first question, and I don't
understand your second question at all. The answer to the
first question is "one." I will only have one stream open at a
time in this context.


oh sorry about that, my question was to a broad "you audience" of which
"you" and "me" are subsets :)

Basically how many streams can we have at one time, and are these like
little tributaries to a river? std out, ofstream derive from ostream
etc. but in the end are these streams combined or they all go different
ways?

Suppose standard output stream can be copied and various copies exist.
If we flush each of them using endl, will we observe a defined
behavior?

Jan 16 '06 #4
I have one and only one question: How do you COPY N characters in
an input stream into a char buffer[N] array? I'd appreciate an answer
to that question.

--RY

Jan 16 '06 #5
Randy wrote:
I have one and only one question: How do you COPY N characters in
an input stream into a char buffer[N] array? I'd appreciate an answer
to that question.


Have you tried implementing it yourself? I'd look into 'sgetn' and all
the positioning functions for the stream buffer... The idea is to first
preserve the position of the buffer by calling

pos_type pos = os.pubseekoff(0, ...); // look up the other arguments

then

int got = os.sgetn(where, N);

and then restore the position

os.pubseekpos(pos, ..

Of course you can run into a streambuf implementation that can't really
set its position, but for files it should work fine.

V
Jan 16 '06 #6
Victor Bazarov <v.********@comAcast.net> writes:
Randy wrote:
I have one and only one question: How do you COPY N characters in
an input stream into a char buffer[N] array? I'd appreciate an answer
to that question.
Have you tried implementing it yourself?


Yes, albeit not very hard. I guess I'm waffling between doing that and
thinking there must be a better way.

As I said, I also can't see a way to get at the actual character
pointer that must be buried somewhere. I suppose I could create a
class that derives from streambuf() and then expose the

Has NO one EVER wanted to "peek" (at more than one character at a
time) into an extractor stream???? I'm amazed the istream class
doesn't provide this function "out-of-the-box."

I mentioned in another post *why* I'm trying to do this:

I'm creating a set of classes for some functions that require
lots of parameters. A simple, human-readable and -controllable
way to do this is to overload the inserter and extractor
operators to report and control these parameters as
property/value pairs, e.g., "ParameterA=value1".

The idea is to feed a list of property/value pairs to the top-level
class, have his extractor interpret and set the properties that
pertain to it (and ignore those that don't), then pass the entire set
to the child classes, the children to their grandchildren, etc.
(speaking hierarchically and/or inheritance-wise).

So, I need to operate on an istream without wasting the
string (or stream) in it.

Is this so unusual? Am I mis-using the extractor class or abusing the
operators somehow by wanting to do this?
I'd look into 'sgetn' and all
the positioning functions for the stream buffer... The idea is to first
preserve the position of the buffer by calling

pos_type pos = os.pubseekoff(0, ...); // look up the other arguments

then

int got = os.sgetn(where, N);

and then restore the position

os.pubseekpos(pos, ..

Of course you can run into a streambuf implementation that can't really
set its position, but for files it should work fine.


That's just it - these aren't necessarily file streams, and I've found,
at least within the istream class, that tellg() and seekg() don't work
with non-file streams. I'm thinking these gets and seeks won't either.
--
% Randy Yates % "Midnight, on the water...
%% Fuquay-Varina, NC % I saw... the ocean's daughter."
%%% 919-577-9882 % 'Can't Get It Out Of My Head'
%%%% <ya***@ieee.org> % *El Dorado*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
Jan 17 '06 #7
Randy Yates wrote:
[...]
Has NO one EVER wanted to "peek" (at more than one character at a
time) into an extractor stream???? I'm amazed the istream class
doesn't provide this function "out-of-the-box."


What's the point? Extract all available characters into a string-
based stream and peek (peer) as much as you need.

Character-based streams only have characters. And either they are
empty, or you can extract one and work on it. If your streams are
not file-based, extracting a single character can probably trigger
extraction of the next one. How would you peek at others?

Can you describe why you'd need to peek at more than one and what
is the reason extracting all available chars into a separate buffer
and working with it doesn't solve your problem?

V
Jan 17 '06 #8
"Victor Bazarov" <v.********@comAcast.net> writes:
Randy Yates wrote:
[...]
Has NO one EVER wanted to "peek" (at more than one character at a
time) into an extractor stream???? I'm amazed the istream class
doesn't provide this function "out-of-the-box."


What's the point? Extract all available characters into a string-
based stream and peek (peer) as much as you need.

Character-based streams only have characters. And either they are
empty, or you can extract one and work on it. If your streams are
not file-based, extracting a single character can probably trigger
extraction of the next one. How would you peek at others?

Can you describe why you'd need to peek at more than one and what
is the reason extracting all available chars into a separate buffer
and working with it doesn't solve your problem?


Yes.

I need to peek at more than one character at a time because I'm trying
to match a string of the form "propertyX=valueY." I need to peek rather
than extract the characters because, if any one overloaded extractor
doesn't match the property/value pair being passed, the string should
stay "in the stream" for searching by other class instances (other
class member elements within a class).

It's like this. I've got a set of enumeration classes that are derived
from a CENUM(propertyName, comma-delimited-property-value-list) class that
defines a set of property/value pairs. Any one class, say, classA, may contain
several of these enumeration classes as member elements. For example,

class Cars : CENUM
{
CENUM("CARS", "Mustang, Pinto, Maverick"); /* am I showing my age? */
}

class Trucks : CENUM
{
CENUM("TRUCKS", "Mack, International");
}

class Vehicles
{
Cars cars;
Trucks trucks;
AnotherClass ac;
friend istream& operator>> (istream& is, Vehicles& vehicles);
}

I want to be able to "parse" a set of property/value pairs coming down
an istream like this:

istream& operator>> (istream& is, Vehicles& vehicles)
{
is >> vehicles.cars;
is >> vehicles.trucks;
is >> vehicles.ac;

return is;
}

So if I wanted to set the trucks parameter to "International", I
can simply stream the string "TRUCKS=International" into the
vehicles class.

I can't extract all available chars in any one overloaded extractor
because if the property/value pair doesn't match, I want it to
remain in the stream for passing to subsequent classes.

Does that help explain the predicament?
--
% Randy Yates % "Watching all the days go by...
%% Fuquay-Varina, NC % Who are you and who am I?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% <ya***@ieee.org> % *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jan 17 '06 #9
Randy Yates wrote:
[..]
I can't extract all available chars in any one overloaded extractor
because if the property/value pair doesn't match, I want it to
remain in the stream for passing to subsequent classes.

Does that help explain the predicament?


It's not a predicament. It's a bad design and misuse of streams.
Extract as much as you can into a separate buffer and make your
"overloaded extractor" and "subsequent classes" work with that buffer
instead of streams. Problem solved.

V
Jan 17 '06 #10
Randy Yates wrote:
I can't extract all available chars in any one overloaded extractor
because if the property/value pair doesn't match, I want it to
remain in the stream for passing to subsequent classes.
Does that help explain the predicament?


No. If you need a function, class, or whatever that does something you can
write it. Demanding his inclusion in the standard library and waiting for
compilers to implement it takes too much or infinite time.

--
Salu2
Jan 17 '06 #11
It may be a misuse of streams, but it damn sure is handy.

Here's the solution:

void peekline(istream& is, string& strLine)
{
char line[CEnum::TOKEN_MAX_CHARS];
int ch;
uint16_t n;
uint16_t m;

ch = 1;
for (n = 0; (n < CEnum::TOKEN_MAX_CHARS) && (ch != -1); n++)
{
ch = is.rdbuf()->sbumpc();
line[n] = *((char*)&ch + 0);
}

for (m = 0; m < n; m++)
{
is.rdbuf()->sungetc();
}

line[n-1] = 0;
strLine = line;
}

Jan 17 '06 #12
TB
Randy sade:
It may be a misuse of streams, but it damn sure is handy.

Here's the solution:

void peekline(istream& is, string& strLine)
{
char line[CEnum::TOKEN_MAX_CHARS];
int ch;
uint16_t n;
uint16_t m;

ch = 1;
for (n = 0; (n < CEnum::TOKEN_MAX_CHARS) && (ch != -1); n++)
{
ch = is.rdbuf()->sbumpc();
And if sbumpc() returns traits::eof()?
line[n] = *((char*)&ch + 0);
}

for (m = 0; m < n; m++)
{
is.rdbuf()->sungetc();


Same here.

--
TB @ SWEDEN
Jan 17 '06 #13
> Has NO one EVER wanted to "peek" (at more than one character at a
time) into an extractor stream???? I'm amazed the istream class
doesn't provide this function "out-of-the-box."


You seem to want a stream to behave like an array of bytes.
If you want an array of bytes, read from the stream and write to an
array of bytes.

A stream is not necessarily something you can peek into. If this stream
wraps over a network connection, after bytes are read from the
operating system's buffers, they are effectively "lost".

--
Pedro Lamarão

Jan 17 '06 #14

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

Similar topics

3
by: Elena | last post by:
I need to extract data from a recordset. I have to find the identifier of the table and add 1 to it. In VB6, I used a recordset, as in the code below: 'get a new item number strsql = "Select...
0
by: Sunil Basu | last post by:
Hi, I have a interesting thing to know and discuss with you. I am extracting data from an Excel file in a Delphi DbGrid through SQL. I want to create a criteria on a specific cell value of the...
0
by: runner7 | last post by:
I used file_get_contents() to read a pdf into a string and then tried to extract the encoded part between the "stream" and "endstream" words using the strpos() and substr() functions. (I could not...
0
by: sgsiaokia | last post by:
I need help in extracting data from another source file using VBA. I have problems copying the extracted data and format into the required data format. And also, how do i delete the row that is not...
1
by: JSagar | last post by:
Hello Expert ! I am new to python , i had done 'C' Code , which extracted file data , But i don't want to give my exe On remote side. Following thing is it Possible using python? -...
3
by: Kourosh | last post by:
Hi all, I'm trying to call a COM function from C# The function gets a paramter of type IStream in C++. I've found the type System.Runtime.InteropServices.ComTypes.IStream, however I'm not sure...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.