Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791411
  • 博文数量: 290
  • 博客积分: 10653
  • 博客等级: 上将
  • 技术积分: 3178
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-24 23:08
文章存档

2013年(6)

2012年(15)

2011年(25)

2010年(86)

2009年(52)

2008年(66)

2007年(40)

分类: C/C++

2007-11-10 14:12:57

文件: 示例函数.rar
大小: 0KB
下载: 下载
C++I/O流类库

1I/O 流类库的功能

  C++ 语言中的输入输出功能是由I/O流类库提供的。所谓流,就是数据或信息的流动。

流有两种,输出流和输入流 I/O设备流到内存的是输入流。从内存流到某种设备叫输出流。

2。数据流对象

   数据流被定义为对象,标准输入输出流的对象由系统指定。

 标准输入流对象 Cin  用来从键盘上流入数据。称输入操作。

 标准输出流对象Cout用来将输出流中的数据显示在屏幕上。称输出操作。

 

常用的读写操作——用来对输入输出流进行操作。主要是屏幕输出和键盘输入操作。

 

屏幕输出操作:

1.  使用预定义的插入符 <<

<<  是左移位运算符的重载。重载后的运算符优先级,结合性,操作数的个数不变。

该插入符有两个操作数, 表示

 ,操作数1  <<   操作数2                           :将 什么 流向 何处。

 

操作数1 表明流的去处, 也就是输出流对象。

操作数2 表明流出的内容,通常是表达式。

如:cout<<”hello”;

     Hello 数据流向屏幕,即显示输出。

 

使用插入符的注意事项

1,  可穿接多个插入符,用来输出多个数据项。实际上,cout ostream类的对象。

iostream.h头文件中它被定义为全局对象。

class _CRTIMP iostream : public istream, public ostream {

public:

        iostream(streambuf*);

        virtual ~iostream();

protected:

        iostream();

        iostream(const iostream&);

inline iostream& operator=(streambuf*);

inline iostream& operator=(iostream&);

private:

        iostream(ios&);

        iostream(istream&);

        iostream(ostream&);

};

inline iostream& iostream::operator=(streambuf* _sb)

 { istream::operator=(_sb); ostream::operator=(_sb); return *this; }

 

inline iostream& iostream::operator=(iostream& _strm)

 { return operator=(_strm.rdbuf()); }

2.  使用插入符时,应该注意输出表达式中运算符的优先级如果低于左移位运算符的优先级时应加()括起表达式。如:

                       Cout<j ? I :j <

     由于左移运算符的优先级高于三目运算符,因此编译时出错。应该将三目运算表达式用括号括起来。

 

 

使用成员函数PUT()输出一个字符。

 

格式:ostream & cout.put(char c );

                     参数char c 用来指定待输出的字符的,该函数返回ostream类的对象引用。Cout 用来引用成员函数 put的输出流对象名。表示显示屏幕。

如:

Cout.put(‘A’)将字符输出到当前光标处。等同于语句: cout<<’A’;

 

使用成员函数write()输出字符串

 

格式:cout.write(const char *str, int n)

 

Str  用来存放字符串的字符指针或是字符数组的名字;也可用以字符串常量;

Int n 指定输出字符串中的字符个数。当参数为strlenstr)时,表示输出整个字符串。      

;

键盘输入操作:  插入符  >> 右移位运算符的重载。

使用和输出操作符大同小义,请参照上述。

 

使用成员函数GET()获取一个字符

 Char get() 不带参数的Get()函数。在类istream中。

 该函数从指定的输入流中提取一个字符,包括空白符,并返回该字符。遇到输入流中文件结束符时,返回,EOF  #define EOF -1。键盘输入字符流的时候,结束符用ctrl +z

Void main()

{

       Char ch;

       Cout<<”enter characters: “<

      While((ch=cin.get())!=EOF)

              Cout.put(ch);

}

格式:istream & get( char c )

 

使用成员函数getline() 获取一行字符

可以从输入流中读入一行字符或多个字符。

  Getlinechar * buf int n char deline=’\n’

该函数的参数:

 

Buf 是一个字符指针或是字符数组,用来存放从输入流中读取的若干字符。

N   iny型数 ,用来限定从输入流中读取的字符个数。包括空格,不超过 n-1个。

系统自动插入一个空字符。

 Char  用来规定字符的终止符。默认值为\n

 

 该函数的结束条件如下:

    从输入流中读取到n-1个字符后。

    从输入流中读取到换行符后

    从输入流中读取到文件结束符或者是其他输入流的结束符。

 

class _CRTIMP istream : virtual public ios {

 

public:

    istream(streambuf*);

    virtual ~istream();

 

    int  ipfx(int =0);

    void isfx() { unlockbuf(); unlock(); }

 

    inline istream& operator>>(istream& (__cdecl * _f)(istream&));

    inline istream& operator>>(ios& (__cdecl * _f)(ios&));

    istream& operator>>(char *);

    inline istream& operator>>(unsigned char *);

    inline istream& operator>>(signed char *);

    istream& operator>>(char &);

    inline istream& operator>>(unsigned char &);

    inline istream& operator>>(signed char &);

    istream& operator>>(short &);

    istream& operator>>(unsigned short &);

    istream& operator>>(int &);

    istream& operator>>(unsigned int &);

    istream& operator>>(long &);

    istream& operator>>(unsigned long &);

    istream& operator>>(float &);

    istream& operator>>(double &);

    istream& operator>>(long double &);

    istream& operator>>(streambuf*);

 

    int get();

 

    inline istream& get(         char *,int,char ='\n');

    inline istream& get(unsigned char *,int,char ='\n');

    inline istream& get(  signed char *,int,char ='\n');

 

    istream& get(char &);

    inline istream& get(unsigned char &);

    inline istream& get(  signed char &);

 

    istream& get(streambuf&,char ='\n');

    inline istream& getline(         char *,int,char ='\n');

    inline istream& getline(unsigned char *,int,char ='\n');

    inline istream& getline(  signed char *,int,char ='\n');

 

    inline istream& ignore(int =1,int =EOF);

    istream& read(char *,int);

    inline istream& read(unsigned char *,int);

    inline istream& read(signed char *,int);

 

    int gcount() const { return x_gcount; }

    int peek();

    istream& putback(char);

    int sync();

 

    istream& seekg(streampos);

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

    streampos tellg();

 

    void eatwhite();

 

protected:

    istream();

    istream(const istream&);    // treat as private

    istream& operator=(streambuf* _isb); // treat as private

    istream& operator=(const istream& _is) { return operator=(_is.rdbuf()); }

    istream& get(char *, int, int);

     int do_ipfx(int);

 

private:

    istream(ios&);

    int getint(char *);

    int getdouble(char *, int);

    int _fGline;

    int x_gcount;

};

 

    inline istream& istream::operator>>(istream& (__cdecl * _f)(istream&))

 { (*_f)(*this); return *this; }

    inline istream& istream::operator>>(ios& (__cdecl * _f)(ios&)) { (*_f)(*this);

return *this; }

 

    inline istream& istream::operator>>(unsigned char * _s)

{ return operator>>((char *)_s); }

    inline istream& istream::operator>>(  signed char * _s)

{ return operator>>((char *)_s); }

 

    inline istream& istream::operator>>(unsigned char & _c)

 { return operator>>((char &) _c); }

    inline istream& istream::operator>>(  signed char & _c)

 { return operator>>((char &) _c); }

 

    inline istream& istream::get(         char * _b, int _lim, char _delim)

 { return get(        _b, _lim, (int)(unsigned char)_delim); }

    inline istream& istream::get(unsigned char * _b, int _lim, char _delim)

{ return get((char *)_b, _lim, (int)(unsigned char)_delim); }

    inline istream& istream::get(signed   char * _b, int _lim, char _delim)

 { return get((char *)_b, _lim, (int)(unsigned char)_delim); }

 

    inline istream& istream::get(unsigned char & _c) { return get((char &)_c); }

    inline istream& istream::get(  signed char & _c) { return get((char &)_c); }

 

    inline istream& istream::getline(         char * _b,int _lim,char _delim)

 { lock(); _fGline++; get(        _b, _lim, (int)(unsigned char)_delim); unlock();

return *this; }

    inline istream& istream::getline(unsigned char * _b,int _lim,char _delim)

{ lock(); _fGline++; get((char *)_b, _lim, (int)(unsigned char)_delim);

 unlock(); return *this; }

    inline istream& istream::getline(  signed char * _b,int _lim,char _delim)

{ lock(); _fGline++; get((char *)_b, _lim, (int)(unsigned char)_delim);

unlock(); return *this; }

 

    inline istream& istream::ignore(int _n,int _delim)

{ lock(); _fGline++; get((char *)0, _n+1, _delim); unlock(); return *this; }

 

    inline istream& istream::read(unsigned char * _ptr, int _n)

 { return read((char *) _ptr, _n); }

    inline istream& istream::read(  signed char * _ptr, int _n)

 { return read((char *) _ptr, _n); }

 

 

阅读(4035) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~