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

copy of stream

Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil
Jul 22 '05 #1
7 3286
Philipp posted:
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil

You can't copy a stream. The Standard achieves this by making the copy
constructor private.

-JKop
Jul 22 '05 #2

"Philipp" <no*************@hotmail.com> schrieb im Newsbeitrag
news:41******@epflnews.epfl.ch...
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil


Why do you want to do this? There's better solutions to your problem,
I think.
Jul 22 '05 #3
Gernot Frisch wrote:
"Philipp" <no*************@hotmail.com> schrieb im Newsbeitrag
news:41******@epflnews.epfl.ch...
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil

Why do you want to do this? There's better solutions to your problem,
I think.


Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.
Jul 22 '05 #4
Philipp wrote:

Gernot Frisch wrote:
"Philipp" <no*************@hotmail.com> schrieb im Newsbeitrag
news:41******@epflnews.epfl.ch...
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil

Why do you want to do this? There's better solutions to your problem,
I think.


Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.


Could you eleborate. What does 'name of the stream' mean.

Is it the stream variables name you want to change.
if yes, then you can create a reference to the original object.

void SomeClass::write(ofstream& originalStream){
ofstream& newStream( originalStream );

newStream << "Hello" << endl;
}

Now the names 'originalStream' and 'newStream' refer to the very
same stream object. But a rewrite would be better.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
>> Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.

That was what I was expecting.
if yes, then you can create a reference to the original object.


and that was what I wanted to suggest.
Jul 22 '05 #6
Karl Heinz Buchegger wrote:
Philipp wrote:
Gernot Frisch wrote:

"Philipp" <no*************@hotmail.com> schrieb im Newsbeitrag
news:41******@epflnews.epfl.ch...
Hello,
How can you copy a stream (or change a reference to it)?

Imagine a function which writes "Hello" to a filestream:

void SomeClass::write(ofstream& originalStream){
ofstream newStream;
newStream = originalStream; // this does not work
newStream << "Hello" << endl;
}

How can I make that right? Thanks for your answers Phil
Why do you want to do this? There's better solutions to your problem,
I think.


Oh I just wanted to change the "name" of the stream so I need not
rewrite some code.

Could you eleborate. What does 'name of the stream' mean.

Is it the stream variables name you want to change.
if yes, then you can create a reference to the original object.

void SomeClass::write(ofstream& originalStream){
ofstream& newStream( originalStream );

newStream << "Hello" << endl;
}

Now the names 'originalStream' and 'newStream' refer to the very
same stream object. But a rewrite would be better.


Exactly what I wanted. I just didn't know you could use a ofstream as
parameter in the ofstream constructor.

Thanks for your time.
Phil
Jul 22 '05 #7
Philipp wrote:

Is it the stream variables name you want to change.
if yes, then you can create a reference to the original object.

void SomeClass::write(ofstream& originalStream){
ofstream& newStream( originalStream );

newStream << "Hello" << endl;
}

Now the names 'originalStream' and 'newStream' refer to the very
same stream object. But a rewrite would be better.


Exactly what I wanted. I just didn't know you could use a ofstream as
parameter in the ofstream constructor.


Man. You need a book.
I am not creating a second ofstream object!
I define a reference to the object!

A reference is not an object. It is just another name
for an object. The very same ofstream object is know
now with 2 different names. That's all. No constructor
is involved.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

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

Similar topics

9
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...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
4
by: martin | last post by:
Hi, I am copying an xml file like so. Dim xmlDoc As New XmlDocument xmlDoc.Load("C:\Program Files\Templates\message.msg") Console.WriteLine("Tmaplate loaded") xmlDoc.Save("C:\Program...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
0
by: NetronProject | last post by:
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types. All entries in the CollectionBase...
22
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
0
by: Al Reynolds | last post by:
Morning, I'm trying to copy a stream from mobical.net to a local file (for those of you who don't know, Mobical is a free online SyncML server, but they don't officially offer iCalendar...
4
by: Herman.Schultz | last post by:
Hi, How can I use iostream library in c++ to copy a file from i th byte to an output file? Thank you.
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
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...
0
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...
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...

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.