• C++: I/O流详解


    一、输入流操作

    1.read 无格式输入指定字节数

    istream& read ( char* pch, int nCount );

    istream& read ( unsigned char* puch, int nCount );

    istream& read ( signed char* psch, int nCount );

    2.get 从流中提取字符,包括空格

    int get();

    istream& get( char* pch, int nCount, char delim = ' ' );

    istream& get( unsigned char* puch, int nCount, char delim = ' ' );

    istream& get( signed char* psch, int nCount, char delim = ' ' );

    istream& get( char& rch ); istream& get( unsigned char& ruch );

    istream& get( signed char& rsch );

    istream& get( streambuf& rsb, char delim = ' ' );

    3.getline 从流中提取一行字符

    istream& getline( char* pch, int nCount, char delim = ' ' );

    istream& getline( unsigned char* puch, int nCount, char delim = ' ' );

    istream& getline( signed char* psch, int nCount, char delim = ' ' );

    4.ignore 提取并丢弃流中指定字符

    istream& ignore( int nCount = 1, int delim = EOF );

    5.peek 返回流中下一个字符,但不从流中删除

    int peek();

    6.gcount 统计最后输入的字符个数

    int gcount() const;

    7.eatwhite 忽略前导空格

    void eatwhite();

    8.seekg 移动输入流指针

    istream& seekg( streampos pos );

    istream& seekg( streamoff off, ios::seek_dir dir );

    9.tellg 返回输入流中指定位置的指针值

    streanpos tellg();

    10.operator >> 提取运算符

    basic_istream& operator>>( basic_istream& (*pf)(basic_istream&));

    basic_istream& operator>>( basic_ios<E, T>& (*pf)(basic_ios<E, T>&));

    basic_istream& operator>>( ios_base<E, T>& (*pf)(ios_base<E, T>&));

    basic_istream& operator>>( basic_streambuf<E, T> *sb);

    basic_istream& operator>>(bool& n);

    basic_istream& operator>>(short& n);

    basic_istream& operator>>(unsigned short& n);

    basic_istream& operator>>(int& n);

    basic_istream& operator>>(unsigned int& n);

    basic_istream& operator>>(long& n);

    basic_istream& operator>>(unsigned long& n);

    basic_istream& operator>>(void *& n);

    basic_istream& operator>>(float& n);

    basic_istream& operator>>(double& n);

    basic_istream& operator>>(long double& n);

    例子:

     1 //用get函数从键盘输入字符
     2 void f1(){
     3     char c;
     4     cout<<"Enter first sentence followed by enter
    ";
     5     while((c = cin.get())!='
    ')
     6         cout.put(c);
     7     cout<<endl;
     8     
     9     cout<<"Enter second sentence followed by enter
    ";
    10     while(cin.get(c)){
    11         if (c == '
    ')
    12             break;
    13         cout.put(c);    
    14     }    
    15     cout<<endl;
    16     
    17     cout<<"Enter thired sentence followed by enter
    ";
    18     char s[80];
    19     cin.get(s, 10);
    20     cout<<s<<endl;
    21 }

    输出:

    二、输出流

     

    1.put 无格式,插入一个字节

    ostream& put(char ch);

    2.write 从无格式,插入一字节序列

    ostream& write( const char* pch, int nCount );

    ostream& write( const unsigned char* puch, int nCount );

    ostream& write( const signed char* psch, int nCount );

    3.flush 刷新输出流

    ostream& flush();

    4.seekp 移动输出流指针

    ostream& seekp( streampos pos );

    ostream& seekp( streamoff off, ios::seek_dir dir );

    5.tellp 返回输出流中指定位置的指针

    streampos tellp();

    6.operstor<< 插入运算符

    basic_ostream& operator<<( basic_ostream& (*pf)(basic_ostream&));

    basic_ostream& operator<<( basic_ios<E, T>& (*pf)(basic_ios<E, T>&));

    basic_ostream& operator<<( ios_base<E, T>& (*pf)(ios_base<E, T>&));

    basic_ostream& operator<<( basic_streambuf<E, T> *sb);

    basic_ostream& operator<<(const char *s);

    basic_ostream& operator<<(char c);

    basic_ostream& operator<<(bool n);

    basic_ostream& operator<<(short n);

    basic_ostream& operator<<(unsigned short n);

    basic_ostream& operator<<(int n);

    basic_ostream& operator<<(unsigned int n);

    basic_ostream& operator<<(long n);

    basic_ostream& operator<<(unsigned long n);

    basic_ostream& operator<<(float n);

    basic_ostream& operator<<(double n);

    basic_ostream& operator<<(long double n);

    basic_ostream& operator<<(void *n);

    例子:

    1 #include<iostream.h>
    2 void main()
    3 { cout << "Enter a sentence followed by Enter
    " ;
    4   char s[ 26 ] ;
    5   cin.getline ( s, 26 ) ;
    6   cout.write(s, 26) ;
    7   cout<<endl;
    8 }

    输出:

     三、流错误状态

  • 相关阅读:
    大话数据结构笔记
    zsh安装教程
    Matlab安装教程
    7-16 插入排序还是归并排序 (25 分)
    7-14 插入排序还是堆排序 (25 分)
    7-14 二叉搜索树的最近公共祖先 (30 分)
    7-11 笛卡尔树 (25 分)
    中缀转换为后缀和前缀
    7-15 水果忍者 (30 分)
    兔子的区间密码(思维)
  • 原文地址:https://www.cnblogs.com/llxblogs/p/7768353.html
Copyright © 2020-2023  润新知