• C++流操纵算子(格式控制)



           输入/输出的数据没有指定格式,它们都按缺省的格式输入/输出。然而,有时需要对数据格式进行控制。这时需利用ios类中定义的格式控制成员函数,通过调用它们来完成格式的设置。


    ios类的格式控制函数如下所示:


    long flags( ) const 返回当前的格式标志。
    long flays(long newflag) 设置格式标志为newflag,返回旧的格式标志。
    long setf(long bits)    设置指定的格式标志位,返回旧的格式标志。
    long setf(long bits,long field) 将field指定的格式标志位置为bits,返回旧的格式标志。
     long unsetf(long bits)  清除bits指定的格式标志位,返回旧的格式标志。
    long fill(char c)   设置填充字符,缺省条件下是空格。
      char fill( )   返回当前填充字符。
    int precision(int val)   设置精确度为val,控制输出浮点数的有效位,返回旧值。
    int precision( ) 返回旧的精确度值。
    int width(int val)       设置显示数据的宽度(域宽),返回旧的域宽。
    int width( )   只返回当前域宽,缺省宽度为0。这时插入操作能按表示数据的最小宽度显示数据。 


    预定义的操纵算子
        使用成员函数控制格式化输入输出时,每个函数调用需要写一条语句,尤其是它不能用在插入或提取运算符的表达式中,而使用操纵算子,则可以在插入和提取运算符的表达式中控制格式化输入和输出。在程序中使用操纵算字必须嵌入头文件
    iomanip.h

    dec 十进制的输入输出
     hex 十六进制的输入输出
     oct   八进制的输入输出
    ws   提取空白字符
     ends   输出一个nul字符
    endl   输出一个换行字符,同时刷新流
    flush 刷新流
    resetiosflags(long) 请除特定的格式标志位
    setiosflags(long)  设置特定的格式标志位
    setfill(char) 设置填充字符
    setprecision(int) 设置输出浮点数的精确度
     setw(int) 设置域宽格式变量

    其它流函数

    错误处理
        在对一个流对象进行I/O操作时,可能会产生错误。当错误发生时,错误的性质被记录在ios类的一个数据成员中。
    ios类中定义的描述错误状态的常量:

    goodbit  没有错误,正常状态  eofbit 到达流的结尾   
    failbit I/O操作失败,清除状态字后,可以对流继续进行操作。
    badbit 试图进行非法操作,清除状态字后,流可能还可以使用。
    hardfail 致命错误,不可恢复的错误。


    ostream类的成员函数
    流的其它成员函数可以从流中读取字符或字符串,对流进行无格式化的输入 输出操作,以及直接控制对流的I/O操作。

    返回类型 ios类的成员 描      述
    ostream* tie(ostream*)    将当前流与指定的输出流连接起来。每当需要 读取当前流时,连接的流会自动刷新。C++流库已用cin.tie(cout)将输入流与输出流连接起来。要取消与输出流的连接可采用is.tie(0)
    ostream* tie( ) 返回指向连接流的指针


    返回类型 ostream类的成员 描      述
    ostream& put(char ch) 向流中输出一个字符ch,不进行任何转换
    ostream& write(char*,int) 向流中输出指定长度的字符串,不进行转换
    ostream&  flush( ) 刷新流,输出所有缓冲的但还未输出的数据
    ostream& seekp(streampos) 移动流的当前指针到给定的绝对位置
    ostream& seekp(sereamoff,seek_dir) 流的当前指针类似与文件的当前指针
    streampos teelp( ) 返回流的当前指针的绝对位置

    istream类的成员函数

    返回类型 istream类的成员 描        述
    int get( ) 读取并返回一个字符
    istream& get(char&c) 读取字符并存入c中
    istream& get(char*ptr,int len,char delim='') 读取指定的字符到缓冲区中,直到遇到指定的分界符为止,分界符不填入缓冲区。
    istream& getline(char*ptr,int len,char delim='') 与get(char*ptr,int len,chardelim ='') 类似,但将分界符填入缓冲区。
    istream&  putback( ) 将最近读取的字符放回流中
    istream& read(char*,int) 读取规定长度的字符串到缓冲区中
    int peek( )  返回流中下一个字符,但不移动文件指针
    istream& seekg(streampos) 移动当前指针到一绝对地址
    istream&  seekg(streampos,seek_dir) 移动当前指针到一相对地址
    streampos tellg( ) 返回当前指针
    istream& ignore(int n=1,delim=EOF) 跳过流中几个字符,或直到遇到指定的分界符为止

    关于::符号的探讨

      仔细查找了一下ios类控制符的源文件,可以追溯到头文件ios_base.h,其中包含了很多控制流的变量或方法,而这些成员都被包围在了命名空间std中,代码如下所示。因此,流控制符是std命名空间中的静态常量,::则代表了这些常量来自于std这个命名空间。

     1 // 27.4.2.1.2  Type ios_base::fmtflags
     2     /**
     3      *  @brief This is a bitmask type.
     4      *
     5      *  @c "_Ios_Fmtflags" is implementation-defined, but it is valid to
     6      *  perform bitwise operations on these values and expect the Right
     7      *  Thing to happen.  Defined objects of type fmtflags are:
     8      *  - boolalpha
     9      *  - dec
    10      *  - fixed
    11      *  - hex
    12      *  - internal
    13      *  - left
    14      *  - oct
    15      *  - right
    16      *  - scientific
    17      *  - showbase
    18      *  - showpoint
    19      *  - showpos
    20      *  - skipws
    21      *  - unitbuf
    22      *  - uppercase
    23      *  - adjustfield
    24      *  - basefield
    25      *  - floatfield
    26     */
    27     typedef _Ios_Fmtflags fmtflags;
    28 
    29     /// Insert/extract @c bool in alphabetic rather than numeric format.
    30     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
    31 
    32     /// Converts integer input or generates integer output in decimal base.
    33     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
    34 
    35     /// Generate floating-point output in fixed-point notation.
    36     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
    37 
    38     /// Converts integer input or generates integer output in hexadecimal base.
    39     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
    40 
    41     /// Adds fill characters at a designated internal point in certain
    42     /// generated output, or identical to @c right if no such point is
    43     /// designated.
    44     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
    45 
    46     /// Adds fill characters on the right (final positions) of certain
    47     /// generated output.  (I.e., the thing you print is flush left.)
    48     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
    49 
    50     /// Converts integer input or generates integer output in octal base.
    51     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
    52 
    53     /// Adds fill characters on the left (initial positions) of certain
    54     /// generated output.  (I.e., the thing you print is flush right.)
    55     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
    56 
    57     /// Generates floating-point output in scientific notation.
    58     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
    59 
    60     /// Generates a prefix indicating the numeric base of generated integer
    61     /// output.
    62     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
    63 
    64     /// Generates a decimal-point character unconditionally in generated
    65     /// floating-point output.
    66     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
    67 
    68     /// Generates a + sign in non-negative generated numeric output.
    69     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
    70 
    71     /// Skips leading white space before certain input operations.
    72     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
    73 
    74     /// Flushes output after each output operation.
    75     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
    76 
    77     /// Replaces certain lowercase letters with their uppercase equivalents
    78     /// in generated output.
    79     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
    80 
    81     /// A mask of left|right|internal.  Useful for the 2-arg form of @c setf.
    82     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
    83 
    84     /// A mask of dec|oct|hex.  Useful for the 2-arg form of @c setf.
    85     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
    86 
    87     /// A mask of scientific|fixed.  Useful for the 2-arg form of @c setf.
    88     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
  • 相关阅读:
    AsyncTask类
    linux下带有空格的文件怎么删除
    python 获取指定文件夹的大小
    python 无法获取隐藏文件夹中的文件列表
    LINK : fatal error LNK1104: 无法打开文件“libboost_serialization-vc90-mt-gd-1_62.lib”
    解决错误 fatal error C1010: unexpected end of file while looking for precompiled head
    PCH Warning: header stop cannot be in a macro or #if block.
    C++ Boost在VS2015中的使用
    dev-c++ boost库的安装
    python 获取命令行输出结果
  • 原文地址:https://www.cnblogs.com/johnpher/p/2570596.html
Copyright © 2020-2023  润新知