ignore,getline,get 函数
来源:互联网 发布:淘宝自然搜索排名优化 编辑:程序博客网 时间:2024/05/17 04:17
http://blog.csdn.net/caojunling/archive/2007/11/17/1890519.aspx
有更多粒种子
查ignore资料时在网上找到例子:接着就查了几个与流相关的函数,getline,get
在下面的程序例子中我们用istream 成员函数ignore()来去
掉delimiter 缺省情况下换行符被用作delimiter
字符读取过程一直进行直到以下任何一个条件发生在发生了任何一个条件之后一
个空字符被放在数组中的下一个位置上
size-1 个字符被读入
遇到文件结束符end-of-file
遇到delimiter 字符再次说明它不会被放在数组中而是留作istream 的下一个
字符
get()的返回值是被调用的istream 对象gcount()返回实际被读入的字符个数下面
是其用法的一个简单示例
#include <iostream>
int main()
{
const int max_line = 1024;
char line[ max_line ];
while ( cin.get( line, max_line ))
{
// 最大读取数量max_line - 1, 也可以为null
int get_count = cin.gcount();
cout << "characters actually read: "
<< get_count << endl;
// 处理每一行
// 如果遇到换行符
// 在读下一行之前去掉它
if ( get_count & max_line-1 )
cin.ignore();
}
}
缺省情况下ignore()从被调用的istream 对象中读入一个字符并丢弃掉但是我们也可
以指定显式的长度和delimiter 它的原型如下
ignore( streamsize length = 1, int delim = traits::eof )
ignore()从istream 中读入并丢弃length 个字符或者遇到delimiter 之前包含delimiter
在内的所有字符或者直到文件结尾它返回当前被应用的istream 对象
因为程序员常常忘了在应用get()之前丢弃delimiter 所以使用成员函数getline()要比get()
更好因为它丢弃delimiter 而不是将其留作istream 的下一个字符
cin为全局对象,使用getline方法
istream::getline
public member functionistream& getline (char* s, streamsize n );istream& getline (char* s, streamsize n, char delim );
Get line from stream
Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.
Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '/n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it. If you don't want this character to be extracted, you can use member get instead.
The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.
The number of characters read by this function can be obtained by calling to the member function gcount.
A global function with the same name exists in header <string>. This global function provides a similar behavior, but with standard C++ string objects instead of c-strings: see getline (string).
Parameters
- s
- A pointer to an array of characters where the string is stored as a c-string.
- n
- Maximum number of characters to store (including the terminating null character).
This is an integer value of type streamsize.
If the function stops reading because this size is reached, the failbit internal flag is set. - delim
- The delimiting character. The operation of extracting succesive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '/n' (a newline character) to be the delimiting character.
Return Value
The function returns *this.
Errors are signaled by modifying the internal state flags:
This is also set if the function stops extracting because n-1 characters were extracted (n including the terminating null-character).
Notice that some eofbit cases will also set failbit.badbitAn error other than the above happened.
Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
// istream getline#include <iostream>using namespace std;int main () { char name[256], title[256]; cout << "Enter your name: "; cin.getline (name,256); cout << "Enter your favourite movie: "; cin.getline (title,256); cout << name << "'s favourite movie is " << title; return 0;}
This example ilustrates how to get lines from the standard input stream ( cin ).
Basic template member declarations
( basic_istream<charT,traits> )
typedef charT char_type;basic_istream& getline (char_type* s, streamsize n );basic_istream& getline (char_type* s, streamsize n, char_type delim );
See also
istream::getGet unformatted data from stream (public member function)istream::ignoreExtract and discard characters (public member functions)istream::gcountGet number of characters extracted by last unformatted input operation (public member function)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
getline为全局方法
getlinefunctionistream& getline ( istream& is, string& str, char delim );istream& getline ( istream& is, string& str );
<string>
Get line from stream
Extracts characters from is and stores them into str until a delimitation character is found.
The delimiter character is delim for the first function version, and '/n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
Parameters
- is
- istream object on which the extraction operation is performed.
- str
- string object where the extracted content is stored.
- delim
- The delimiting character. The operation of extracting succesive characters is stopped when this character is read.
Return Value
The same as parameter is.
Errors are signaled by modifying the internal state flags:
Additionaly, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
// getline with strings#include <iostream>#include <string>using namespace std;int main () { string str; cout << "Please enter full name: "; getline (cin,str); cout << "Thank you, " << str << "./n";}
This example ilustrates how to get lines from the standard input stream ( cin ).
Basic template member declarations
( basic_istream<charT,traits> )
template<class charT, class traits, class Allocator> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Allocator>& str, charT delim );template<class charT, class traits, class Allocator> basic_istream<charT,traits>& getline (basic_istream<charT,traits>& is, basic_string<charT,traits,Allocator>& str );
See also
operator>>Extract string from istream (function)istream::getlineGet line from stream (public member function)/////////////////////////////////////////////////////////////////////////////////////////////////////////////get不丢弃分隔符,该分隔符当作下次提取字符,getline则丢弃分隔符
istream::getpublic member functionint get();istream& get ( char& c );istream& get ( char* s, streamsize n );istream& get ( char* s, streamsize n, char delim );istream& get ( streambuf& sb);istream& get ( streambuf& sb, char delim );
Get unformatted data from stream
These member functions perform unformatted input operations. Depending on the type and number of arguments the function behaves in the following way:
- int get();
- Extracts a character from the stream and returns its value (casted to an integer).
- istream& get ( char& c );
- Extracts a character from the stream and stores it in c.
- istream& get (char* s, streamsize n );
- Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '/n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline if you want this character to be extracted (and discarded).
The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s. - istream& get (char* s, streamsize n, char delim );
- Same as above, except that the delimiting character is the one specified indelim instead of '/n'.
- istream& get (streambuf& sb);
- Extracts characters from the stream and inserts them in the stream buffer sb until either the delimiting character '/n' is found or end of file is reached. The extraction also stops if an error occurs either in the input sequence controled by the stream or in the output sequence controlled by sb.
- istream& get (streambuf& sb, char delim );
- Same as above, except that the delimiting character is the one specified indelim instead of '/n'.
The number of characters read by any of the previous input operations can be obtained by calling to the member function gcount.
Parameters
- c
- A char variable to store the extracted character.
- s
- A pointer to an array of characters where the string is stored as a c-string
- n
- Maximum number of characters to store (including the ternimating null character).
This is an integer value of type streamsize. - delim
- The delimiting character. The operation of extracting succesive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '/n' (a newline character) to be the delimiting character.
- sb
- An output stream buffer (an object of class streambuf or any of its derived classes).
Return Value
For the first prototype, the function returns the character read. For the remaining prototypes, the function return *this.
Errors are signaled by modifying the internal state flags:
Notice that some eofbit cases will also set failbit.badbitAn error other than the above happened.
Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
// istream get#include <iostream>#include <fstream>using namespace std;int main () { char c, str[256]; ifstream is; cout << "Enter the name of an existing text file: "; cin.get (str,256); is.open (str); // open file while (is.good()) // loop while extraction from file is possible { c = is.get(); // get character from file if (is.good()) cout << c; } is.close(); // close file return 0;}
This example prompts for the name of an existing text file and prints its content on the screen.
Basic template member declarations
( basic_istream<charT,traits> )
typedef charT char_type;int_type get();basic_istream& get (char_type& c );basic_istream& get (char_type* s, streamsize n );basic_istream& get (char_type* s, streamsize n, char_type delim );basic_istream& get (basic_streambuf<char_type,traits>& sb);basic_istream& get (basic_streambuf<char_type,traits>& sb, char_type delim );
See also
istream::getlineGet line from stream (public member function)istream::ignoreExtract and discard characters (public member functions)istream::gcountGet number of characters extracted by last unformatted input operation (public member function)
////////////////////////////////////////////////////////
istream::ignorepublic member functionsistream& ignore ( streamsize n = 1, int delim = EOF );
Extract and discard characters
Extracts characters from the input sequence and discards them.
The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.
Parameters
- n
- Maximum number of characters to extract (and ignore).
This is an integer value of type streamsize. - delim
- Delimiting character.
Return Value
The function returns *this.Errors are signaled by modifying the internal state flags:
Additionally, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
// istream ignore#include <iostream>using namespace std;int main () { char first, last; cout << "Enter your first and last names: "; first=cin.get(); cin.ignore(256,' '); last=cin.get(); cout << "Your initials are " << first << last; return 0;}
This example ilustrates how after reading the first character with get, the remaining input characters up to the next whitespace character are ignored.
Basic template member declarations
( basic_istream<charT,traits> )typedef traits::int_type int_type;basic_istream& ignore (streamsize n = 1, int_type delim = traits::eof() );
See also
istream::peekPeek next character (public member function)istream::getGet unformatted data from stream (public member function)istream::getlineGet line from stream (public member function)istream::readRead block of data (public member function)istream::readsome- ignore,getline,get 函数
- C++中cin、cin.get()、cin.getline()、getline()、gets()、cin.ignore()函数详解
- cin , cin.get() , cin.getline() , getline() , cin.ignore() 的用法
- get与getline函数
- get()和getline()函数
- 浅析cin.getline,cin.get,cin.ignore,cin.clear用法
- 函数cin.get、 cin.getline 、getline、 gets
- cin.get();cin.getline();getline(cin,string);cin.ignore()的理解
- get和getline函数差别
- c++ getline(),get()函数笔记
- cin.ignore()函数用法及cin.getline()用法举例
- C++中get函数和getline函数
- cin、cin.get()、cin.getline()、getline()、gets()等函數的用法 以及cin.ignore,cin.clear等。。
- [转]浅析cin.getline,cin.get,cin.ignore,cin.clear用法
- cin.getline,cin.get,cin.ignore,cin.clear用法(转)
- C++语法基础--istream,流状态及其影响,get(),getline(),ignore()
- cin、cin.get()、cin.getline()、getline()、gets()函数的用法
- cin.get()cin.getline()getline()gets()等函数的用法
- 在程序中使用DataTable对象(二)
- chm打不开时最简单的办法
- MySQL
- WTL 学习笔记 -- ResizeDialog
- JSP入门
- ignore,getline,get 函数
- WTL 学习笔记 -- CSplitterWindow
- Java常见设计模式应用场景
- png 压缩原理
- WTL 学习笔记 -- DDX 和 DDV
- 关于工作和财富
- WTL 学习笔记 -- ActiveX
- WTL 学习笔记 -- Property Sheets and Wizards
- snake画图