istream buffering

P

Philipp Kraus

Hello,

I have a problem to understand stream data. I have one open istream,
which is valid. I would like to read the data from the stream two
times, but I know I can't read the same position twice. Can I copy the
from the stream in a buffer or anything else in which I can read the
data again? I must duplicated the stream content to work with two
seperated (not referenced) copies.

Thanks for help

Phil
 
K

Kai-Uwe Bux

Philipp said:
Hello,

I have a problem to understand stream data. I have one open istream,
which is valid. I would like to read the data from the stream two
times, but I know I can't read the same position twice. Can I copy the
from the stream in a buffer or anything else in which I can read the
data again? I must duplicated the stream content to work with two
seperated (not referenced) copies.

I think, I am not fully grasping your problem. What would be wrong with
something along the following lines:

int i;
some_file >> i;
int j = i;

Now, you have the read integer in i and in j.


Best

Kai-Uwe Bux
 
T

Trifle Menot

I have a problem to understand stream data. I have one open istream,
which is valid. I would like to read the data from the stream two
times, but I know I can't read the same position twice. Can I copy the
from the stream in a buffer or anything else in which I can read the
data again? I must duplicated the stream content to work with two
seperated (not referenced) copies.

If you're on linux, the "tee" utility can read an input stream and write
it to multiple outputs, which can be fifos. Then you can read the fifos
as separate input streams. If you need a sample shell script I can post
one.
 
P

Philipp Kraus

I think, I am not fully grasping your problem. What would be wrong with
something along the following lines:

int i;
some_file >> i;
int j = i;

I don't know the type of the istream. I get in a class method a
reference to the istream and need the content two times. The istream
can hold a large dataset (> 1GB) (or some string data).
I need a cross-plattform (I use boost) solutions for this structur

myclass::input( std::istream x) {
std::istream y;
copy(x, y); // -> that's my problem

mynextmethod(x, y);
}

x and y must have the same content, but they don't be a reference. The
method "mynextmethod" is declared with:
mynextmethod( std::istream&, std::istream );

Can I call the copy-constructor like std::istream y(x) to copy the data?

Thanks
 
A

Alf P. Steinbach /Usenet

* Philipp Kraus, on 03.07.2010 13:21:
I don't know the type of the istream. I get in a class method a
reference to the istream and need the content two times. The istream can
hold a large dataset (> 1GB) (or some string data).

In this case you copy the stream to a temporary file.

I need a cross-plattform (I use boost) solutions for this structur

myclass::input( std::istream x) {
std::istream y;
copy(x, y); // -> that's my problem

mynextmethod(x, y);
}

The above is not a structure or anything else. It's syntactically invalid.

x and y must have the same content, but they don't be a reference. The
method "mynextmethod" is declared with:
mynextmethod( std::istream&, std::istream );

Can I call the copy-constructor like std::istream y(x) to copy the data?

No, there is no such.

You /can/ copy your stream to a std::istringstream, but doing that for a GiB
worth of data would be irresponsible.

Copy your stream to a temporary file.



Cheers & hth.,

- Alf
 
I

Ian Collins

On 07/ 4/10 10:28 AM, Philipp Kraus wrote:

[your quoting is horrible, don't post HTML to Usenet!]
This is a useful idea, but it may be that once the istream contains very
little data and even very many. Is there a possibility to consider when
it pays not to create a temporary file and when?

You could create your own stream buffer which could make that choice and
manage the temporary file if required.
 
J

James Kanze

On 07/ 4/10 10:28 AM, Philipp Kraus wrote:
You could create your own stream buffer which could make that
choice and manage the temporary file if required.

He could also check whether the stream is seekable. If so, no
copying is needed. (But I'm not sure whether it's always
possible to reliably check.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top