Istream函数汇总及输入函数总结

来源:互联网 发布:儿童上网控制软件 编辑:程序博客网 时间:2024/06/05 19:16

Ø  gcount()

Return values

The extraction count.

 

Remarks

Use basic_istream::get to read unformatted characters.

 

Example

 

Ø  get()

int_type get( );

basic_istream& get(

   char_type& _Ch                

);

basic_istream& get(char_type *_Str,  

   streamsize _Count);

basic_istream& get(

   char_type *_Str,                

   streamsize _Count,            

   char_type _Delim

);

Return values

The parameterless form of get returns the element read as an integer or end of file. The remaining forms return the stream (*this).

Remarks

The first of these unformatted input functions extracts an element, if possible, as if by returning rdbuf->sbumpc.   //遍历指针指向的区域

Otherwise, it returns traits_type::eof. If the function extracts no element, it calls setstate(failbit).

 

The second function extracts the int_type element meta the same way. If meta compares equal to traits_type::eof, the function calls setstate(failbit). Otherwise, it stores traits_type::to_char_type(meta) in _Ch. The function returns *this.

 

The third function returns get(_Str, _Count, widen('/n')).

 

The fourth function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores char_type after any extracted elements it stores.

Example

 

 

Ø  getline()

basic_istream& getline(

   char_type *_Str,

   streamsize _Count

);

basic_istream& getline(

   char_type *_Str,

   streamsize _Count,

   char_type _Delim

);

Return Value

The stream (*this).

Remarks

The first of these unformatted input functions returns getline(_Str, _Count, widen('/n')).

The second function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores the string termination character after any extracted elements it stores. If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit). In any case, it returns *this

Example

Ø  Read_s()

basic_istream& _Read_s(

    char_type *_Str,

    size_t _Str_size,

    streamsize _Count

);

Return Value

The stream (*this).

Remarks

The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. Extraction stops early on end of file, in which case the function calls setstate(failbit). In any case, it returns *this.

Example

 

 

Ø  Readsome_s()

streamsize _Readsome_s(

    char_type *_Str,

    size_t _Str_size,

    streamsize _Count

);

Return Value

The count of items in the buffer.

Remarks

The unformatted input function extracts up to count elements and stores them in the array beginning at _Str. If good is false, the function calls setstate(failbit). Otherwise, it assigns the value of rdbuf->in_avail to N. If N < 0, the function calls setstate(eofbit). Otherwise, it replaces the value stored in N with the smaller of _Count and N, and then calls read(_Str, N). In any case, the function returns gcount.

Example

 

 

Ø  ignore()

basic_istream& ignore(

   streamsize _Count = 1,

   int_type _Delim = traits_type::eof( )

);

Return Value

The stream (*this).

Remarks

The unformatted input function extracts up to _Count elements and discards them. If _Count equals numeric_limits<int>::max, however, it is taken as arbitrarily large. Extraction stops early on end of file or on an element _Ch such that traits_type::to_int_type(_Ch) compares equal to _Delim (which is also extracted).

Example

Ø  gets()

char *gets(

   char* buffer

);

Return Values

Each function returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred.

Remarks

The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('/n').

gets then replaces the newline character with a null character ('/0') before returning the line. In contrast, the fgets function retains the newline character.

Security Remarks

The string to be acquired must not be larger than the maximum number of bytes allowed in buffer; otherwise, a buffer overrun can occur.

This can lead to a denial of service attack against the application if an access violation occurs, or in the worst case, allow an attacker to inject executable code into your process. Consider using an appropriate strsafe function.

Example

 

 

Ø  getchar()

int getchar(

    void

);

Return Values

returns the character read.

To indicate an read error or end-of-file condition, getwchar return WEOF.

Remarks

Each routine reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character.

Example

 

输入函数的总结与对比

语言函数 获取字符个数正常返回值停止输入方式错误返回值 是否会获取结尾space或'/n'C++getvoid1intsetstate(failbit)traits_type::eof NULLone par1the stream (*this)setstate(failbit)traits_type::eof NULLtwo parrestricted the stream (*this)setstate(failbit)/restrictedtraits_type::eof three parrestricted the stream (*this)setstate(failbit)/restrictedtraits_type::eof getlinetwo parrestricted the stream (*this)setstate(failbit)/restricted*this three parrestricted the stream (*this)setstate(failbit)/restricted*this Read_sthree parrestrictedthe stream (*this)setstate(failbit)/restricted*this Readsome_sthree parrestrictedgcount()setstate(failbit)/restrictedgcount()  getsone para lineits argumenterror/end-of-fileNULL  getcharvoid1character readerror/end-of-fileWEOF 注:Readsome_s()只从输入缓存区读取数据,不再接受cin的输入 

 

 

Andy总结于2010年7月10号        22:26:00