关于getline函数的用法,该如何处理
关于getline函数的用法
#include <iostream>
using namespace std;
int main( )
{
char array1[5];
cin.getline(array1,5);
cout < < array1 < <endl;
char array2[5];
cin.getline(array2,5);
cout < <array2 < <endl;
return 0;
}
对于如上代码,如果输入字符超过5个,为什么程序直接退出,而没有执行第二个getline();若将函数getline()换为get则正常。
测试如下:
输入:asdfghj
asdf
Press any key to continue
------解决方案--------------------
get
Syntax:
#include <fstream>
int get();
istream& get( char& ch );
istream& get( char* buffer, streamsize num );
istream& get( char* buffer, streamsize num, char delim );
istream& get( streambuf& buffer );
istream& get( streambuf& buffer, char delim );
The get() function is used with input streams, and either:
* reads a character and returns that value,
* reads a character and stores it as ch,
* reads characters into buffer until num - 1 characters have been read, or EOF or newline encountered,
* reads characters into buffer until num - 1 characters have been read, or EOF or the delim character encountered (delim is not read until next time),
* reads characters into buffer until a newline or EOF is encountered,
* or reads characters into buffer until a newline, EOF, or delim character is encountered (again, delim isn 't read until the next get() ).
For example, the following code displays the contents of a file called temp.txt, character by character:
char ch;
ifstream fin( "temp.txt " );
while( fin.get(ch) )
cout < < ch;
fin.close();
------解决方案--------------------
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. In order of testing, extraction stops:
At end of file.
After the function extracts an element that compares equal to _Delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts _Count - 1 elements.
If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit). In any case, it returns *this.
本来就只能放Count-1个,
而且第一次个数超出后,流状态已经变成failbit了,
所以还要在第一次输入结束后,加上
cin.clear();
第二个cin输入才能有效。
------解决方案--------------------
上面的朋友都说清楚了。
getline的目的是“按行读”,因此不管是没读到还是读了超过了count + 1啊,cin的状态都会被置failbit,从而告诉调用者,从“按行”的意义上讲,上一次读已经出问题了。因为对于“按行”来讲,缓冲区大小不够就是一种“问题”。
而get没有这个问题,它只是按要求去读取一定数目的字符,不管读够了还是没读够,只要读到了,都不应该做为“问题”看待。
#include <iostream>
using namespace std;
int main( )
{
char array1[5];
cin.getline(array1,5);
cout < < array1 < <endl;
char array2[5];
cin.getline(array2,5);
cout < <array2 < <endl;
return 0;
}
对于如上代码,如果输入字符超过5个,为什么程序直接退出,而没有执行第二个getline();若将函数getline()换为get则正常。
测试如下:
输入:asdfghj
asdf
Press any key to continue
------解决方案--------------------
get
Syntax:
#include <fstream>
int get();
istream& get( char& ch );
istream& get( char* buffer, streamsize num );
istream& get( char* buffer, streamsize num, char delim );
istream& get( streambuf& buffer );
istream& get( streambuf& buffer, char delim );
The get() function is used with input streams, and either:
* reads a character and returns that value,
* reads a character and stores it as ch,
* reads characters into buffer until num - 1 characters have been read, or EOF or newline encountered,
* reads characters into buffer until num - 1 characters have been read, or EOF or the delim character encountered (delim is not read until next time),
* reads characters into buffer until a newline or EOF is encountered,
* or reads characters into buffer until a newline, EOF, or delim character is encountered (again, delim isn 't read until the next get() ).
For example, the following code displays the contents of a file called temp.txt, character by character:
char ch;
ifstream fin( "temp.txt " );
while( fin.get(ch) )
cout < < ch;
fin.close();
------解决方案--------------------
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. In order of testing, extraction stops:
At end of file.
After the function extracts an element that compares equal to _Delim, in which case the element is neither put back nor appended to the controlled sequence.
After the function extracts _Count - 1 elements.
If the function extracts no elements or _Count - 1 elements, it calls setstate(failbit). In any case, it returns *this.
本来就只能放Count-1个,
而且第一次个数超出后,流状态已经变成failbit了,
所以还要在第一次输入结束后,加上
cin.clear();
第二个cin输入才能有效。
------解决方案--------------------
上面的朋友都说清楚了。
getline的目的是“按行读”,因此不管是没读到还是读了超过了count + 1啊,cin的状态都会被置failbit,从而告诉调用者,从“按行”的意义上讲,上一次读已经出问题了。因为对于“按行”来讲,缓冲区大小不够就是一种“问题”。
而get没有这个问题,它只是按要求去读取一定数目的字符,不管读够了还是没读够,只要读到了,都不应该做为“问题”看待。