473,569 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic question on streams

I understand that to creat a file output stream for example, I have to
include <fstreamand then:

ofstream fout;

which will create an object of type ofstream that I can use to interact with
a file.

My question concerns cout and cin. Clearly by intuition, these are also
objects? Is it possible to define an object of the same type of cin and cout
and use them similarly? What are those types and what would the definitions
to create those objects look like?

Thanks.

--
Posted via a free Usenet account from http://www.teranews.com

Jul 22 '07 #1
13 1594
John Simeon wrote:
I understand that to creat a file output stream for example, I have to
include <fstreamand then:

ofstream fout;

which will create an object of type ofstream that I can use to interact with
a file.

My question concerns cout and cin. Clearly by intuition, these are also
objects? Is it possible to define an object of the same type of cin and cout
and use them similarly? What are those types and what would the definitions
to create those objects look like?

Look up the class hierarchy for std::ostream. You'll see that ofstream
derives from ostream hence cout is interchangeable with your fout.
Jul 22 '07 #2
"John Simeon" <jo******@gmail .comwrote in message
news:46******** **************@ free.teranews.c om...
>I understand that to creat a file output stream for example, I have to
include <fstreamand then:

ofstream fout;

which will create an object of type ofstream that I can use to interact
with a file.

My question concerns cout and cin. Clearly by intuition, these are also
objects? Is it possible to define an object of the same type of cin and
cout and use them similarly? What are those types and what would the
definitions to create those objects look like?
The definition to create a cin or cout duplicate would be fairly complicated
because you have to talk to the console, however that is done on the OS
level. Like for windows I'd have to attach the console window, send the
output, etc...

However.

cin derives from istream, cout derives from ostream and can be used as such.

Here is a little sample toy program to illistrate how you can use std::cout
as a std::ostream:

#include <iostream>
#include <ostream>
#include <fstream>
#include <string>

void OutputRhyme( std::ostream& Output )
{
Output << "Mary had a little lamb\n"
"Its fleece was white as snow.\n"
"Every where that Mary went\n"
"The lamb was sure to go\n";
}

int main()
{
std::string Choice = "";
while ( Choice != "Q" )
{
while ( Choice != "F" && Choice != "S" && Choice != "Q" )
{
std::cout << "Output to <F>ile or <S>creen. <Qto Quit: ";
std::getline( std::cin, Choice );
}

if ( Choice != "Q" )
{
if ( Choice == "F" )
{
std::ofstream File("Rhyme.txt ");
OutputRhyme( File );
}
else
OutputRhyme( std::cout );
Choice = "";

}
}

}
Jul 22 '07 #3
Wait, are you basically saying that ofstream inherits from ostream and that,
therefore, it should be possible to output to the screen with fout?

How would I do that if so?
"Gianni Mariani" <gi*******@mari ani.wswrote in message
news:46******** *************** @per-qv1-newsreader-01.iinet.net.au ...
John Simeon wrote:
>I understand that to creat a file output stream for example, I have to
include <fstreamand then:

ofstream fout;

which will create an object of type ofstream that I can use to interact
with a file.

My question concerns cout and cin. Clearly by intuition, these are also
objects? Is it possible to define an object of the same type of cin and
cout and use them similarly? What are those types and what would the
definitions to create those objects look like?


Look up the class hierarchy for std::ostream. You'll see that ofstream
derives from ostream hence cout is interchangeable with your fout.


--
Posted via a free Usenet account from http://www.teranews.com

Jul 22 '07 #4

John Simeon <jo******@gmail .comwrote in message...
I take it your statement on the task "not being for beginners" is in
reference to creating a cout or cin look-alike from scratch.

I however would merely like to declare a type of the same opject and use
it
that way.

Would this be appropriate then:

ostream Output;

Output << "Hello World!"
That depends on how you declared/defined 'ostream'. Did you define
'operator<<()'?

Please do not top-post.

Why not just use the standard libraries:

#include <iostream>
#include <sstream>
#include <string>
std::ostringstr eam Output;

void MyFunc(){
Output << " In MyFunc()!";
return;
}

int main(){
Output << "Hello World!";
// ......
std::cout<< Output.str() <<std::endl;
Output.clear(); Output.str("");
Output << " How are you?";
MyFunc();
std::string TheOutput( Output.str() );
std::cout<< TheOutput <<std::endl;
// ......
std::cout<< Output.str() <<std::endl;
return 0;
} // main()

--
Bob R
POVrookie
Jul 23 '07 #5
John Simeon wrote:
Wait, are you basically saying that ofstream inherits from ostream and that,
therefore, it should be possible to output to the screen with fout?

How would I do that if so?
Open the screen "file".

That is platform specific.

If you want to create your own ostream type that writes to a file and
the screen, you can do that too.

G
Jul 23 '07 #6
John Simeon wrote:
Wait, are you basically saying that ofstream inherits from ostream and
that, therefore, it should be possible to output to the screen with fout?
No, he's saying that a std::ostream and a std::ofstream has the same
functions for output, and a std::ofstream can be used in situations where a
std::ostream is expected.
How would I do that if so?
Your first problem is: define screen. C++ has no concept of screens. C++
guarantees that there exists a standard out, but does not say where it ends
and how. On many computers, the screen is a buffer in memory, and writing
to special addresses will make characters appear on the screen. On other
systems, the screen can only be reached with special system calls. How it
works on your system, however is not the topic of this group. But here is
something that works on my system:

std::ofstream out("/dev/stdout");
out << "Hello, world\n";

This works on UNIX systems, where /dev/stdout is a special file. I don't
know if it works, but somethings tell me that the special file name CON has
similar meanings on WinDOS.

--
rbh
Jul 23 '07 #7
"Robert Bauck Hamar" <ro**********@i fi.uio.nowrote in message
news:f8******** **@readme.uio.n o...
John Simeon wrote:
>Wait, are you basically saying that ofstream inherits from ostream and
that, therefore, it should be possible to output to the screen with fout?

No, he's saying that a std::ostream and a std::ofstream has the same
functions for output, and a std::ofstream can be used in situations where
a
std::ostream is expected.
>How would I do that if so?

Your first problem is: define screen. C++ has no concept of screens. C++
guarantees that there exists a standard out, but does not say where it
ends
and how. On many computers, the screen is a buffer in memory, and writing
to special addresses will make characters appear on the screen. On other
systems, the screen can only be reached with special system calls. How it
works on your system, however is not the topic of this group. But here is
something that works on my system:

std::ofstream out("/dev/stdout");
out << "Hello, world\n";

This works on UNIX systems, where /dev/stdout is a special file. I don't
know if it works, but somethings tell me that the special file name CON
has
similar meanings on WinDOS.
It's actually CON: The following program outputs:

Did it work?
Testing

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::ofstream Console("CON:") ;
Console << "Testing\n" ;
std::cout << "Did it work?\n";

return 0;
}

One would expect
Testing
Did it work?
but that's not what was output.
Jul 23 '07 #8
On Jul 23, 3:54 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
wrote:
John Simeon wrote:
Wait, are you basically saying that ofstream inherits from ostream and
that, therefore, it should be possible to output to the screen with fout?

No, he's saying that a std::ostream and a std::ofstream has the same
functions for output, and a std::ofstream can be used in situations where a
std::ostream is expected.
How would I do that if so?

Your first problem is: define screen. C++ has no concept of screens. C++
guarantees that there exists a standard out, but does not say where it ends
and how. On many computers, the screen is a buffer in memory, and writing
to special addresses will make characters appear on the screen. On other
systems, the screen can only be reached with special system calls. How it
works on your system, however is not the topic of this group. But here is
something that works on my system:

std::ofstream out("/dev/stdout");
out << "Hello, world\n";

This works on UNIX systems, where /dev/stdout is a special file. I don't
know if it works, but somethings tell me that the special file name CON has
similar meanings on WinDOS.

--
rbh

Jul 23 '07 #9
On Jul 23, 3:54 am, Robert Bauck Hamar <roberth+n...@i fi.uio.no>
wrote:
John Simeon wrote:
Wait, are you basically saying that ofstream inherits from ostream and
that, therefore, it should be possible to output to the screen with fout?
No, he's saying that a std::ostream and a std::ofstream has
the same functions for output, and a std::ofstream can be used
in situations where a std::ostream is expected.
More generally, except when actually opening or closing the
file, you should use std::ostream&, and not std::ofstream&.
How would I do that if so?
Your first problem is: define screen. C++ has no concept of screens. C++
guarantees that there exists a standard out, but does not say where it ends
and how. On many computers, the screen is a buffer in memory, and writing
to special addresses will make characters appear on the screen. On other
systems, the screen can only be reached with special system calls. How it
works on your system, however is not the topic of this group. But here is
something that works on my system:
std::ofstream out("/dev/stdout");
out << "Hello, world\n";
That's not standard Unix (which only requires "/dev/tty",
"/dev/null" and "/dev/console"), just a common extension. And
even on systems where it's implemented (e.g. Solaris or Linux),
it doesn't write to the screen; it writes to where ever standard
out happens to be redirected.

In general, to write to the screen, you need to use the X
Windows interfaces. "/dev/tty" will output to the terminal
window your process is connected to, IF it is connected to a
terminal window, and "/dev/console" will output to the console
window(s), if there are any.
This works on UNIX systems, where /dev/stdout is a special file. I don't
know if it works, but somethings tell me that the special file name CON has
similar meanings on WinDOS.
I'm not sure about the name, but there is something similar to
"/dev/tty" under Windows, I think.

--
James Kanze (GABI Software) email:ja******* **@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

Jul 23 '07 #10

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

Similar topics

8
2952
by: Ronald Legere | last post by:
The new itertools stuff is pretty cool. One thing that bothers me though is that there seems to be no way to copy an iterator. How does one work around this? Is there a trick that I am missing? Without being able to 'split' an iterator, I can't think of how to do some of the cool things you can do with lazy lists in Haskell. It seems...
3
3478
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not,...
8
2721
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) Basically I have an operation which the input and output for are streams - a function which receives a certain 'chunk' of data each time it runs, and it...
11
1783
by: Kobu | last post by:
I have a question about C's abstract "streams" (that I can't seem to FULLY understand from reading several tutorials). Streams seems to suggest that input can be treated continously if needed. Is this true? So far as I know, I can only get a stream to "flow" when there is a '\r'. At that point the '\r' is turned into a '\n' and the whole...
2
4019
by: bonk | last post by:
Hello how do I connect streams in c# ? Imagine the followung scenario: I have a StreamWriter that writes Text to a Stream. How can I tell that Stream to pass that Data to another Stream (connect it to another output Stream) ? In java I am used to connect streams to each other via thier Constructors. For example: new BufferedWriter(new...
1
2411
by: Chris | last post by:
I'm reading up on streams and I have two articles that seem to conflict with each other. One article describes streams and lists a few of the major ones (FileStream, Memory Stream, Network Stream, etc.). It then goes on to discuss the restriction that streams only read bytes or byte arrays that the .net framework provides a StreamReader...
2
2826
by: Abhishek | last post by:
what are the STDUPDATE, STDERR, STDOUT and STDIN streams and how does one access these streams in C language. I am aware of the function fprintf(FILE *fp, char * format, char *s) which puts the string into the corresponding streams. But can you please tellme where does the content go exactly when we put it into the above streams. In which...
4
6211
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES keys (it'll run on two separate computers). Please notice that - this is...
6
7261
by: Ryan Liu | last post by:
Hi, I have some basic question about NetworkStream, can someone explain to me? Thanks a lot in advance! TcpClient has a GetStream() method return a NetworkStream for read and write. I remember there are 2 streams in Java, one for read, one for write. If I just use synchronous Read() and Write() method:
0
7697
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
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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
8120
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...
1
7672
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...
0
5219
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
3653
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...
1
2113
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
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.