关于流的基本问题 [英] Basic question on streams

查看:59
本文介绍了关于流的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,要创建一个文件输出流,我必须

包含< fstreamand然后:


ofstream fout;


这将创建一个类型ofstream的对象,我可以用来与

a文件进行交互。


我的问题关注cout和cin。显而易见,这些也是

对象?是否可以定义相同类型的cin和cout

的对象并使用它们类似?那些类型是什么,创建这些对象的定义是什么?


谢谢。


-

通过 http://www.teranews的免费Usenet帐户发布.com

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

推荐答案

John Simeon写道:
John Simeon wrote:

我明白要创建一个文件输出流,例如,我必须包含< fstreamand然后:


ofstream fout;


这将创建一个类型为ofstream的对象,我可以使用它来与

a文件进行交互。


我的问题关注cout和cin 。显而易见,这些也是

对象?是否可以定义相同类型的cin和cout

的对象并使用它们类似?那些类型是什么,创建这些对象的定义是什么样的?b $ b?
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?



查找std :: ostream的类层次结构。你会看到ofstream

来自ostream因此cout可与你的fout互换。


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


" John Simeon" < jo ****** @ gmail.com写信息

新闻:46 ********************** @免费.teranews.com ...
"John Simeon" <jo******@gmail.comwrote in message
news:46**********************@free.teranews.com...

>我明白要创建一个文件输出流,例如,我必须包含< fstreamand然后:


ofstream fout;


这将创建一个类型ofstream的对象,我可以使用它来交互

with一个文件。


我的问题是关于cout和cin。显而易见,这些也是

对象?是否可以定义相同类型的cin和

cout的对象并使用它们类似?这些类型是什么以及创建这些对象的

定义是什么样的?
>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?



创建cin或cout副本的定义相当复杂

因为你必须与控制台交谈,不过这样做了在操作系统上

级别。就像Windows一样,我必须连接控制台窗口,发送

输出等......


然而。


cin派生自istream,cout派生自ostream,可以这样使用。


这是一个小样本玩具程序,用于说明如何使用std :: cout

作为std :: ostream:


#include< iostream>

#include< ostream>

#include< fstream>

#include< string>


void OutputRhyme(std :: ostream& Output)

{

输出<< 玛丽有一只小羊羔。

它的羊毛像白雪一样白。

每一个玛丽去的地方都没有。 ;

羔羊肯定会去\\&;

}


int main()

{

std :: string Choice ="" ;;

while(Choice!=" Q")

{

while(Choice!=" F"&& Choice!=""&& Choice!=" Q")

{

std :: cout<< 输出到< F> ile或< S> creen。 < Qto Quit:" ;;

std :: getline(std :: cin,Choice);

}


if(Choice!=" Q")

{

if(Choice ==" F")

{

std :: ofstream文件(" Rhyme.txt");

OutputRhyme(文件);

}

else

OutputRhyme(std :: cout);

选择="" ;;


}

}


}

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 = "";

}
}

}


等等,你基本上是说ofstream继承自ostream而且,

因此,应该可以用fout输出到屏幕上吗?


如果是这样我该怎么办?

Gianni Mariani < gi ******* @ mariani.wswrote in message

news:46 *********************** @ per-qv1-newsreader-01.iinet.net.au ...
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*******@mariani.wswrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...

John Simeon写道:
John Simeon wrote:

>我明白要创建一个文件输出流,例如,我必须包含< fstreamand然后:

of of fout;

将创建一个类型为ofstream的对象,我可以用它与文件进行交互。

我的问题涉及cout和cin。显而易见,这些也是对象?是否可以定义相同类型的cin和
cout的对象并使用它们类似?这些类型是什么以及创建这些对象的
定义是什么样的?
>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?




查找std :: ostream的类层次结构。你会看到ofstream

来自ostream因此cout可以与你的fout互换。



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




-

通过免费的Usenet帐户从 http://www.teranews.com



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


这篇关于关于流的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆