“cpp/io/basic ios/setstate”的版本间的差异

来自cppreference.com
< cpp‎ | io‎ | basic ios
 
第1行: 第1行:
{{cpp/io/basic_ios/title | setstate}}
+
{{cpp/io/basic_ios/title|setstate}}
 
{{cpp/io/basic_ios/navbar}}
 
{{cpp/io/basic_ios/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl |  
+
{{dcl|  
 
void setstate( iostate state );
 
void setstate( iostate state );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
 在当前已设置表之外,设置流错误状态标志 {{tt|state}} 。实质上调用 {{c|clear(rdstate() {{!}} state)}} 。可能抛出异常。
+
 在当前已设置表之外,设置流错误状态标志 {{|state}}。实质上调用 {{c|clear(rdstate() {{!}} state)}}。可能抛出异常。
  
 
===参数===
 
===参数===
 
{{par begin}}
 
{{par begin}}
{{par | state |要设置的流错误状态标志。 为下列常量的组合:
+
{{par | state |要设置的流错误状态标志。 为下列常量的组合:
 
{{cpp/io/ios_base/iostate consts}}
 
{{cpp/io/ios_base/iostate consts}}
 
}}
 
}}
第21行: 第21行:
 
===示例===
 
===示例===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <iostream>
 
#include <iostream>
 
#include <sstream>
 
#include <sstream>
第30行: 第29行:
 
     std::ostringstream stream;
 
     std::ostringstream stream;
  
     if (!stream.fail()) {
+
     if (!stream.fail())
 
         std::cout << "stream is not fail\n";
 
         std::cout << "stream is not fail\n";
    }
 
  
 
     stream.setstate(std::ios_base::failbit);
 
     stream.setstate(std::ios_base::failbit);
  
     if (stream.fail()) {
+
     if (stream.fail())
 
         std::cout << "now stream is fail\n";
 
         std::cout << "now stream is fail\n";
    }
 
  
     if (!stream.good()) {
+
     if (!stream.good())
 
         std::cout << "and stream is not good\n";
 
         std::cout << "and stream is not good\n";
    }
 
 
}
 
}
| output=
+
|output=
 
stream is not fail
 
stream is not fail
 
now stream is fail
 
now stream is fail
第52行: 第48行:
 
===参阅===
 
===参阅===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/io/basic_ios/dsc rdstate}}
+
{{dsc inc|cpp/io/basic_ios/dsc rdstate}}
{{dsc inc | cpp/io/basic_ios/dsc clear}}
+
{{dsc inc|cpp/io/basic_ios/dsc clear}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|en|es|fr|it|ja|pt|ru}}
 
{{langlinks|de|en|es|fr|it|ja|pt|ru}}

2023年12月13日 (三) 11:07的最后版本

 
 
 
 
void setstate( iostate state );

在当前已设置表之外,设置流错误状态标志 state。实质上调用 clear(rdstate() | state)。可能抛出异常。

目录

[编辑] 参数

state - 要设置的流错误状态标志。可为下列常量的组合:
常量 解释
goodbit 无错误
badbit 不可恢复的流错误
failbit 输入/输出操作失败(格式化或提取错误)
eofbit 关联的输出序列已抵达文件尾


[编辑] 返回值

(无)

[编辑] 示例

#include <iostream>
#include <sstream>
 
int main()
{
    std::ostringstream stream;
 
    if (!stream.fail())
        std::cout << "stream is not fail\n";
 
    stream.setstate(std::ios_base::failbit);
 
    if (stream.fail())
        std::cout << "now stream is fail\n";
 
    if (!stream.good())
        std::cout << "and stream is not good\n";
}

输出:

stream is not fail
now stream is fail
and stream is not good

[编辑] 参阅

返回状态标志
(公开成员函数) [编辑]
修改状态标志
(公开成员函数) [编辑]