Stream陈述问题 [英] Stream states questions

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

问题描述

我正在阅读TC ++ PL3和21.3.3 Stream State,4个成员函数

返回bool被提及:

template< class Ch ,类Tr = char_traits< Ch

class basic_ios:public ios_base {

public:

// ...

bool good()const; //下一步操作可能成功

bool eof()const; //看到输入结束

bool fail()const; //下一个操作将失败

bool bad()const; //流已损坏


[...]

};


还提到:


"如果状态良好(),则先前的输入操作成功。如果

状态为good(),则下一个输入操作可能会成功;否则,

将失败。


==将输入操作应用于不在商品中的流()

状态是一个空操作,只要读入的变量是




Q1:上述含义是什么?

=="如果我们尝试读入变量v并且操作失败,则


Q2:这是否意味着fail()在此之后变为true ?

" v的值应保持不变(如果v是由istream或ostream成员函数处理的类型之一的变量

则不变)。状态fail()和bad()之间的差异很小。当状态

失败()但也不坏()时,假设流是未损坏的流并且没有丢失任何字符。当状态为

bad()时,所有投注均关闭。

Q3:当!good()为真时,fail()是否为真?

解决方案

* john:


我正在阅读TC ++ PL3和21.3。 3流状态,4个成员函数

返回bool被提及:


模板<类Ch,类Tr = char_traits< Ch

class basic_ios:public ios_base {

public:

// ...

bool good()const; //下一步操作可能成功

bool eof()const; //看到输入结束

bool fail()const; //下一个操作将失败

bool bad()const; //流已损坏


[...]

};


还提到:


"如果状态良好(),则先前的输入操作成功。如果

状态为good(),则下一个输入操作可能会成功;否则,

将失败。


==将输入操作应用于不在商品中的流()

状态是一个空操作,只要读入的变量是




Q1:上述含义是什么?



这意味着在一个ungood状态下,流输入和输出操作根本没有任何东西(除非可能抛出异常,如果你有的话)转了

那个。)


=="如果我们尝试读入变量v并且操作失败,


Q2:这是否意味着fail()在此之后变为真?



是。


" v的值应保持不变(它是如果v是由istream或ostream成员函数处理的类型之一的变量

,则不变。状态fail()和bad()之间的差异很小。当状态

失败()但也不坏()时,假设流是未损坏的流并且没有丢失任何字符。当状态为

bad()时,所有投注均关闭。


Q3:当!good()为true时,失败()始终为true ?



No.


考虑到fail()在一组可能中检查一位有帮助

问题标志:badbit,eofbit和failbit。 good()不是单独的

位,并且它不是倒置的失败位:good()表示/ all /

三个问题位为零。 alles in ordnung。


但是请注意,自动设置eof()的唯一方法是,无法在文件末尾读取
,这往往是还设置了失败位。


另请注意,operator void *(用于转换为逻辑布尔值)

和运算符! (同上)只检查一下failbit。


换句话说,让我们成为一个流对象,然后如果(s){...}不是

与if(s.good()){...}相同,它与if(!s.fail()){...}相同。这是非常令人困惑的
。但是,请记住,你可以对iostreams说不。


干杯,& hth。,


- Alf


2007-09-08 15:39,john写道:
< blockquote class =post_quotes>
我正在阅读TC ++ PL3和21.3.3 Stream State,4个成员函数

返回bool被提及:


模板< class Ch,类Tr = char_traits< Ch

class basic_ios:public ios_base {

public:

// ...

bool good()const; //下一步操作可能成功

bool eof()const; //看到输入结束

bool fail()const; //下一个操作将失败

bool bad()const; //流已损坏


[...]

};


还提到:


"如果状态良好(),则先前的输入操作成功。如果

状态为good(),则下一个输入操作可能会成功;否则,

将失败。


==将输入操作应用于不在商品中的流()

状态是一个空操作,只要读入的变量是




Q1:上述含义是什么?



这意味着如果你试图从一个文件读取变量''value''和

good()== false ''value''中的数据不会被更改。


=="如果我们尝试读入变量v并且操作失败,则


Q2:这是否意味着fail()在此之后变为真?



是。


" v的值应保持不变(如果v是,则不变)由istream或ostream成员函数处理的其中一种类型的变量

)。状态fail()和bad()之间的差异很小。当状态

失败()但也不坏()时,假设流是未损坏的流并且没有丢失任何字符。当状态为

bad()时,所有投注均关闭。


Q3:当!good()为true时,失败()始终为true ?



是的,有三个与流状态相关联的标志,eof,

失败和坏。如果没有设置那么good()== true。


-

Erik Wikstr?m


在2007-09-08 16:17,Erik Wikstr?m写道:


2007-09-08 15:39,john写道:


>我正在阅读TC ++ PL3并在21.3.3流状态中,提到4个成员函数
返回bool:

模板< class Ch,类Tr = char_traits< Ch
class basic_ios:public ios_base {
public:
// ...
bool good( )const; //下一个操作可能会成功
bool eof()const; //看到输入的结尾
bool fail()const; //下一个操作将失败
bool bad()const; //流已损坏

[...]
};

还提到:

如果国家好()前一个输入操作成功。如果
状态为good(),则下一个输入操作可能会成功;否则,它将失败。

==将输入操作应用于不在good()
状态的流是一个空操作,只要变量是阅读是关注的。

Q1:上述意味着什么?



这意味着如果你试图从一个文件中读取变量''value''和

good()== false ''value''中的数据不会被更改。


> =="如果我们尝试读入变量v并且操作失败, ;

Q2:这是否意味着fail()在此之后成为现实?



是。


>" v的值应保持不变(如果不改变则v是由istream或ostream成员函数处理的类型之一的变量。状态fail()和bad()之间的区别是微妙的。当状态
失败()但也不坏()时,假设流未被破坏且没有字符丢失。当状态为坏()时,所有投注都会被关闭。

Q3:当!good()为真时,fail()总是为真吗?



是的,有三个与流状态相关的标志,eof,

失败和坏。如果没有设置那么good()== true。



更正,上面的句子应该是不,有三个标志......。


- -

Erik Wikstr?m


I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions
returning bool are mentioned:
template <class Ch, class Tr= char_traits<Ch
class basic_ios: public ios_base {
public:
// ...
bool good() const; // next operation might succeed
bool eof() const; // end of input seen
bool fail() const; // next operation will fail
bool bad() const; // stream is corrupted

[...]
};

It is also mentioned:

"If the state is good() the previous input operation succeeded. If the
state is good(), the next input operation might succeed; otherwise, it
will fail.

==Applying an input operation to a stream that is not in the good()
state is a null operation as far as the variable being read into is
concerned."

Q1: What does the above mean?
=="If we try to read into a variable v and the operation fails,"

Q2: Does this mean that fail() becomes true after this?
"the value of v should be unchanged (it is unchanged if v is a variable
of one of the types handled by istream or ostream member functions). The
difference between the states fail() and bad() is subtle. When the state
is fail() but not also bad(), it is assumed that the stream is
uncorrupted and that no characters have been lost. When the state is
bad(), all bets are off".
Q3: When !good() is true, is fail() always true?

解决方案

* john:

I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions
returning bool are mentioned:
template <class Ch, class Tr= char_traits<Ch
class basic_ios: public ios_base {
public:
// ...
bool good() const; // next operation might succeed
bool eof() const; // end of input seen
bool fail() const; // next operation will fail
bool bad() const; // stream is corrupted

[...]
};

It is also mentioned:

"If the state is good() the previous input operation succeeded. If the
state is good(), the next input operation might succeed; otherwise, it
will fail.

==Applying an input operation to a stream that is not in the good()
state is a null operation as far as the variable being read into is
concerned."

Q1: What does the above mean?

It means that in an ungood state, stream input and output operations do
nothing at all (except possibly throwing exceptions, if you have turned
that on).

=="If we try to read into a variable v and the operation fails,"

Q2: Does this mean that fail() becomes true after this?

Yes.

"the value of v should be unchanged (it is unchanged if v is a variable
of one of the types handled by istream or ostream member functions). The
difference between the states fail() and bad() is subtle. When the state
is fail() but not also bad(), it is assumed that the stream is
uncorrupted and that no characters have been lost. When the state is
bad(), all bets are off".
Q3: When !good() is true, is fail() always true?

No.

It helps to consider that fail() checks one bit in a set of possible
problem flags: badbit, eofbit and failbit. good() is not a separate
bit, and it''s not the inverted fail bit: good() says that /all/ the
three problem bits are zero. Alles in ordnung.

But note that the only way eof() is set automatically, is by failing to
read beyond end of file, which tends to also set the fail bit.

Also note that operator void* (used for conversion to logical boolean)
and operator! (ditto) just check the failbit.

In other words, let s be a stream object, then if(s){...} is not the
same as if(s.good()){...}, it''s the same as if(!s.fail()){...}. It''s
all very perplexing. But, remember, you can just say NO to iostreams.

Cheers, & hth.,

- Alf


On 2007-09-08 15:39, john wrote:

I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions
returning bool are mentioned:
template <class Ch, class Tr= char_traits<Ch
class basic_ios: public ios_base {
public:
// ...
bool good() const; // next operation might succeed
bool eof() const; // end of input seen
bool fail() const; // next operation will fail
bool bad() const; // stream is corrupted

[...]
};

It is also mentioned:

"If the state is good() the previous input operation succeeded. If the
state is good(), the next input operation might succeed; otherwise, it
will fail.

==Applying an input operation to a stream that is not in the good()
state is a null operation as far as the variable being read into is
concerned."

Q1: What does the above mean?

It means that if you try to read from a file into a variable ''value'' and
good() == false the data in ''value'' will not be changed.

=="If we try to read into a variable v and the operation fails,"

Q2: Does this mean that fail() becomes true after this?

Yes.

"the value of v should be unchanged (it is unchanged if v is a variable
of one of the types handled by istream or ostream member functions). The
difference between the states fail() and bad() is subtle. When the state
is fail() but not also bad(), it is assumed that the stream is
uncorrupted and that no characters have been lost. When the state is
bad(), all bets are off".
Q3: When !good() is true, is fail() always true?

Yes, there are three flags associated with the state of the stream, eof,
fail, and bad. If none of those are set then good() == true.

--
Erik Wikstr?m


On 2007-09-08 16:17, Erik Wikstr?m wrote:

On 2007-09-08 15:39, john wrote:

>I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions
returning bool are mentioned:
template <class Ch, class Tr= char_traits<Ch
class basic_ios: public ios_base {
public:
// ...
bool good() const; // next operation might succeed
bool eof() const; // end of input seen
bool fail() const; // next operation will fail
bool bad() const; // stream is corrupted

[...]
};

It is also mentioned:

"If the state is good() the previous input operation succeeded. If the
state is good(), the next input operation might succeed; otherwise, it
will fail.

==Applying an input operation to a stream that is not in the good()
state is a null operation as far as the variable being read into is
concerned."

Q1: What does the above mean?


It means that if you try to read from a file into a variable ''value'' and
good() == false the data in ''value'' will not be changed.

>=="If we try to read into a variable v and the operation fails,"

Q2: Does this mean that fail() becomes true after this?


Yes.

>"the value of v should be unchanged (it is unchanged if v is a variable
of one of the types handled by istream or ostream member functions). The
difference between the states fail() and bad() is subtle. When the state
is fail() but not also bad(), it is assumed that the stream is
uncorrupted and that no characters have been lost. When the state is
bad(), all bets are off".
Q3: When !good() is true, is fail() always true?


Yes, there are three flags associated with the state of the stream, eof,
fail, and bad. If none of those are set then good() == true.

Correction, the above sentence should be "No, there are three flags...".

--
Erik Wikstr?m


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

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