-
2017-04-17 16:11:05
getline函数输入一整行字符
调用方法:
cin.getline(字符数组(或字符指针),字符个数n,终止标志字符)默认是以“\n”为终止标字符,即终止标志字符可以不写。
特别注意:用getline函数从输入流读字符时,遇到终止标志字符时结束,指针移到该终止标志字符之后,下一个getline函数将该终止标志的下一个字符开始接着读入,如果使用cin.get函数从输入流读字符时,遇到终止字符时停止读取,指针不向后移动,仍然停留在原位置。下一次读取时仍然从该终止标志字符开始。这是getline和get函数的不同之处。
eof函数
eof是end of file的缩写,表示“文件结束”。从输入流读取数据,如果到达文件末尾(遇文件结束符),eof函数值为非零值(表示真),否则为0(假)。
这个函数很有用经常用到!!!
peek函数
peek是观察的意思,其作用是观测下一个字符,调用形式是:
c=cin.peek();
cin.peek函数的返回值是指针指向的当前字符,但它只是观测,指针仍停留在当前位置。如果要访问的字符是文件结束符,则函数值返回的是EOF(-1)。
putback函数
其调用形式是:
cin.putback(ch);
其作用是将前面用get或getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针位置,以供后面读取。
ignore函数
其调用形式是:
cin.ignore(n,终止字符);
函数作用是跳过输入流中n个字符,或在遇到指定的终止字符时提前结束(此时跳过包括终止字符在内的若干字符)。如:
ignore(5,'A') //跳过输入流中的5个字符,遇到'A'就不再跳了 //也可以不带参数或只带一个参数 ignore() //(n默认是1,终止字符默认为EOF) //相当于ignore(1,EOF)
更多相关内容 -
C++中与输入相关的istream类成员函数简介
2020-09-03 07:56:27主要介绍了C++中与输入相关的istream类成员函数简介,包括eof函数和peek函数以及putback函数还有ignore函数,需要的朋友可以参考下 -
c++中istream类的超详细说明
2021-04-16 09:32:44根据前文,istream类是c++标准输入流的一个基类,本篇详细介绍istream类的主要成员函数用法。 1.istream的构造函数 从istream头文件中截取一部分关于构造函数的声明和定义,如下: public: explicit basic_istream...根据前文,istream类是c++标准输入流的一个基类,本篇详细介绍istream类的主要成员函数用法。
1.istream的构造函数
从istream头文件中截取一部分关于构造函数的声明和定义,如下:
public: explicit basic_istream(__streambuf_type* __sb) : _M_gcount(streamsize(0)) { this->init(__sb); } protected: basic_istream() : _M_gcount(streamsize(0)) { this->init(0); } #if __cplusplus >= 201103L basic_istream(const basic_istream&) = delete; basic_istream(basic_istream&& __rhs) : __ios_type(), _M_gcount(__rhs._M_gcount) { __ios_type::move(__rhs); __rhs._M_gcount = 0; } // 27.7.3.3 Assign/swap basic_istream& operator=(const basic_istream&) = delete; basic_istream& operator=(basic_istream&& __rhs) { swap(__rhs); return *this; }
可以看到istream类的默认构造函数是保护类型,而带参数的构造函数则是公有的,根据public和protected的功能,我们要定义一个istream对象,必须要在参数中传入streambuf类型的指针才可以,否则会报编译错误。
一个可用的例子如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; istream iii(&buf); return 0; }
这里应该有人会疑惑,怎么构造函数传的是filebuf类型的入参呢,原因是streambuf的构造函数也是保护类型,且只有一个无参构造函数,所以streambuf是不能直接定义一个对象的,需要使用它的继承者stringbuf或者filebuf,这里使用了filebuf。
另外需要注意的是istream类的拷贝构造函数和赋值函数也都是保护类型的,所以istream是不允许拷贝或者赋值的,所以它也不能直接作为返回类型和参数传递,很多时候需要使用引用来进行传递。
2.右移位>>操作符
部分重载>>操作符函数原型如下:
//重载一系列>>操作符,读取各种数据类型的数据放入输入流中 __istream_type& operator>>(__istream_type& (*__pf)(__istream_type&)) { return __pf(*this); } __istream_type& operator>>(__ios_type& (*__pf)(__ios_type&)) { __pf(*this); return *this; } __istream_type& operator>>(ios_base& (*__pf)(ios_base&)) { __pf(*this); return *this; } __istream_type& operator>>(bool& __n) { return _M_extract(__n); } __istream_type& operator>>(short& __n); __istream_type& operator>>(unsigned short& __n) { return _M_extract(__n); } __istream_type& operator>>(int& __n); __istream_type& operator>>(unsigned int& __n) { return _M_extract(__n); } __istream_type& operator>>(long& __n) { return _M_extract(__n); } __istream_type& operator>>(unsigned long& __n) { return _M_extract(__n); } #ifdef _GLIBCXX_USE_LONG_LONG __istream_type& operator>>(long long& __n) { return _M_extract(__n); } __istream_type& operator>>(unsigned long long& __n) { return _M_extract(__n); } #endif __istream_type& operator>>(float& __f) { return _M_extract(__f); } __istream_type& operator>>(double& __f) { return _M_extract(__f); } __istream_type& operator>>(long double& __f) { return _M_extract(__f); }
>>操作符可用于从缓冲区提取数据并存储在变量中,使用例子如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; //aaa.txt是文本文件,内容是1234 if ( buf.open("aaa.txt", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char c = 0x00; int i = 0; is >> c >> i; cout << "c=" << c << endl << "i=" << i << endl; return 0; }
输出结果如下:
c=1 i=234
到这里,其实看到is这个变量的用法很熟悉,就是我们常用的cin的用法,因为cin它就是istream类型的对象嘛,这里我们可以大概猜测一下cin是怎么实现的,比如:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("/proc/self/fd/0", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char c = 0x00; int i = 0; is >> c >> i; cout << "c=" << c << endl << "i=" << i << endl; return 0; }
执行程序以后,会等待输入,然后在输出,如下:
123 c=1 i=23
从键盘输入123,回车以后,输出了我们想要的结果,这样就实现了跟cin一样的功能。
/proc/self/fd/0是linux系统中标准输入文件,所以打开这个文件操作的话,反映在命令中中,就是在等待输入。
3.get函数
istream头文件中截取get函数声明,如下:
//从输入流中读取一个字符(包括空白字符)并返回,若遇到结束符则返回eof() int_type get(); //从输入流中读取一个字符并存储在引用参数__C中,如果遇到文件结束符,则__C为eof(),返回this指针 __istream_type& get(char_type& __c); //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果在读取字符的过程中遇到了__delim所代表的字符,则提前返回,也就是说__delim相当于是一个终止字符 __istream_type& get(char_type* __s, streamsize __n, char_type __delim); //从输入流中读取字符存储在__s指向的内存中,直到输入流被读取完或者读到了__n-1个字符才返回,其中如果遇到换行符,则提前返回,从实现看,可见就是上面那个函数的终止字符是换行符 __istream_type& get(char_type* __s, streamsize __n) { return this->get(__s, __n, this->widen('\n')); } //从输入流中读取字符存储在streambuf对象__sb中,与终止字符__delim返回 __istream_type& get(__streambuf_type& __sb, char_type __delim); //同理,是以上函数终止字符为换行符 __istream_type& get(__streambuf_type& __sb) { return this->get(__sb, this->widen('\n')); }
get函数部分使用例子如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("/proc/self/fd/0", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char g1 = 0x00; g1 = is.get(); cout << "g1=" << g1 << endl; is.ignore();//该函数是用于忽略换行符,避免下一次读取会读到换行符 char g2 = 0x00; is.get(g2); cout << "g2=" << g2 <<endl; is.ignore(); char g3[12] = {0}; is.get(g3, sizeof(g3), 'n'); cout << "g3=" << g3 <<endl; return 0; }
4.getline函数用法
getline函数原型如下:
//读取一行的字符串放入__s指向的内存中,遇到终止字符__delim提前结束 __istream_type& getline(char_type* __s, streamsize __n, char_type __delim); //读取一行的字符串放入__s指向的内存中,遇到换行符提前结束,相当于直接读取一行了 __istream_type& getline(char_type* __s, streamsize __n) { return this->getline(__s, __n, this->widen('\n')); }
用法如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("/proc/self/fd/0", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char g3[12] = {0}; is.getline(g3, sizeof(g3)); cout << "g3=" << g3 <<endl; return 0; }
这里就是使用了重载的第二个getline函数,默认遇换行符结束。
此时我们输入:1234567,结果如下:1234567 g3=1234567
5.ignore函数和peek函数
函数原型如下:
//忽略输入流中的__n个字符,遇到字符__delim停止忽略并返回 __istream_type& ignore(streamsize __n, int_type __delim); //忽略输入流中的__n个字符 __istream_type& ignore(streamsize __n); //忽略输入流中字符 __istream_type& ignore(); //查看输入流中的下一个字符,但不会从输入流中取出来,字符指针位置也不会发生变化,就是看一眼 int_type peek();
使用方法如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("/proc/self/fd/0", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char g1 = 0x00; char g3[12] = {0}; is >> g1; cout << "g1=" << g1 << endl; is.ignore(2); is.ignore(); char g2 = 0x00; g2 = is.peek(); cout << "g2=" << g2 << endl; is.get(g3, sizeof(g3)); cout << "g3=" << g3 << endl; return 0; }
操作结果如下:
1234567 g1=1 g2=5 g3=567
从这里我们可以知道ignore()不带参数的是忽略一个字符,带参数就是忽略n个字符,而peek只是取出了字符,但并没有移动字符指针。
6.read函数和readsome函数
//读取__n长度的字符串保存在__s中,直到读取完成__n个字符或者遇到文件结束符,eofbit及failbit都被置为1 __istream_type& read(char_type* __s, streamsize __n); /*提取字符存储在__s中,能提取多少取决于streambuf缓冲区中剩余的字符数,查看剩余字符数可使用rdbuf()->in_avail(),rdbuf()就是缓冲区,in_avail()返回还有多少没有处理的字符,rdbuf()->in_avail()取值有如下几种情况: -1 说明遇到文件结束符或者没有可提取字符 0 说明无可提取字符 >0 可提取min(rdbuf()->in_avail(), __n)个字符 readsome函数返回实际提取的字符数 */ streamsize readsome(char_type* __s, streamsize __n);
使用例子如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("/proc/self/fd/0", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char szRead[12] = {0}; is.read(szRead, sizeof(szRead)); cout << "szRead=" << szRead << endl; return 0; }
如果键盘输入不够12个字符,read函数读取不会返回,知道读取12个字符为止。
而如果read函数换成readsome函数,就会直接返回,并不会等待输入,也就是目前缓冲区有多少那么读多少,没有也不等待。7.putback函数、unget函数、sync函数
函数原型如下:
//将前面从输入流中读取的字符__C返回到输入流,插入到当前指针位置,注意返回的字符一定要是之前读取过的,否则是不起作用的 __istream_type& putback(char_type __c); //恢复上一个被读取的字符,重新放回到输入流中,恢复到它原本所在的位置 __istream_type& unget(); //刷新streambuf缓冲区,丢弃剩余没有读取的字符 int sync(); //返回上一次从流中提取的字节数 streamsize gcount() const { return _M_gcount; }
实例如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("aaa.txt", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char aa = 0x00; char szRead[12] = {0}; int i = 0; is >> aa; cout << "aa=" << aa << endl; is >> aa; cout << "aa=" << aa << endl; is.putback('1'); is >> aa; cout << "aa=" << aa << endl; is.unget(); is >> i; cout << "i=" << i << endl; return 0; }
文件aaa.txt内容是1234,输出结果如下:
aa=1 aa=2 aa=1 i=134
从结果可以看出putback可以放回之前提取的任意一个字符,而unget是直接放回上一个提取的字符。
8.tellg和seekg函数
//返回当前字符指针的位置 pos_type tellg(); //根据参数跳转当前字符指针位置,默认按照开始位置跳转 __istream_type& seekg(pos_type); //根据参数跳转当前字符指针位置,ios_base::seekdir标示从什么位置开始跳转 __istream_type& seekg(off_type, ios_base::seekdir);
一个实际使用案例如下:
#include <iostream> #include <fstream> using namespace std; int main() { filebuf buf; if ( buf.open("aaa.txt", ios::in) == nullptr ) { cout << "打开文件出错" << endl; return -1; } istream is(&buf); char aa = 0x00; char bb = 0x00; aa = is.get(); bb = is.get(); cout << "tellg=" << is.tellg() << endl; is.seekg(1, ios::beg);//从文件开始处跳转一个位置 cout << "tellg=" << is.tellg() << endl; return 0; }
结果如下:
tellg=2 tellg=1
到这里,istream类的public成员函数就介绍完毕啦,若有不对之处,欢迎指正。
-
其他istream类方法(get(),getline(),ignore())
2020-12-07 17:31:17get(char&) 和 get() 简介: 代码: #include <iostream> using namespace std;...int main(int argc, char const *argv[]) ...) 将输入的字符赋给其参数(不会跳过空字符————空格、制表符、换行符) ...get(char&) 和 get()
简介:
代码:#include <iostream> using namespace std; int main(int argc, char const *argv[]) { /* get(char&) 将输入的字符赋给其参数(不会跳过空字符————空格、制表符、换行符) */ char c1, c2, c3; cin.get(c1).get(c2).get(c3); cout << c1 << c2 << c3 << endl; //可以拼接使用 /* get(void) 将输入字符转换为整型(通常是int) 注意:get(void)的返回值不是cin,而是int,因此不能拼接使用 */ int i; i = cin.get(); cout << i << endl; i = cin.get(c1).get(c2).get(c3).get(); //valid get()虽然不能拼接使用,但是它可以放在最后 cout << c1 << c2 << c3 << i << endl; cout << EOF; return 0; }
输出:
ABCDEFGH
ABC
68
EFG72
-1
getline()和get()
- istream & get(char *, int, char);
- istream & get(char *, int);
- istream & getline(char *, int, char);
- istream & getline(char *, int);
两者的区别:get()将分界字符留在输入队列中,而getline()不保留
代码:
#include <iostream> using namespace std; int main(int argc, char const *argv[]) { const int Limit = 255; char input[Limit]; cout << "Enter a string for getline() processing:\n"; cin.getline(input, Limit, '#'); cout << "Here is your input:\n"; cout << input << "\nDone with phase 1\n"; char ch; cin.get(ch); cout << "The next input character is " << ch << endl;//getline()函数将丢弃输入中的分界字符# if (ch != '\n') // 读取并丢弃接下来的Limit个字符或直到到达第一个换行符 // 默认参数(1,EOF) cin.ignore(Limit, '\n'); cout << "Enter a string for get() processing:\n"; cin.get(input, Limit, '#'); cout << "Here is your input:\n"; cout << input << "\nDone with phase 2\n"; cin.get(ch); cout << "The next input character is " << ch << endl;//get()函数不会丢弃输入中的分界字符# return 0; }
输出:
Enter a string for getline() processing:
I am a handsome boy!
you know? #hhhh
Here is your input:
I am a handsome boy!
you know?
Done with phase 1
The next input character is h
Enter a string for get() processing:
I am a handsome boy!
you know? #hhhh
Here is your input:
I am a handsome boy!
you know?
Done with phase 2
The next input character is # -
istream类的其他成员函数
2020-04-18 11:29:44eof函数 作用:end of file ,文件结束,从输入流读取数据; 返回值:如果到达文件末尾(遇到文件结束符),eof函...//相当于ignore(1,EOF) 补充 以上介绍的各个成员函数,可以用cin流对象和istream类的其他流对象调用eof函数
作用:end of file ,文件结束,从输入流读取数据;
返回值:如果到达文件末尾(遇到文件结束符),eof函数值为非零值,否则为假;
示例:#include<iostream> using namespace std; int main() { char c; while (!cin.eof()) { if ((c = cin.get()) != ' ') { cout.put(c); } } return 0; }
peek函数
作用:观测下一个字符;
用法:c = cin.peek();
返回值:指针指向的当前字符;
说明:它知识观测,指针仍然停留在当前位置,并不后移;如果观测的字符是文件结束符,则函数值为EOF(即-1);putback函数
作用:将前面用get或getline函数从输入流中读取的字符ch返回到输入流,插入到当前指针位置;
用法:cin.putback(ch);
示例:#include<iostream> using namespace std; int main() { char c[20]; int ch; cout << "please enter a sentence:" << endl; cin.getline(c, 15, '/'); cout << "The first part is:" << c << endl; ch = cin.peek(); cout << "The next character is:" << ch << endl; cin.putback(c[0]); cin.getline(c, 15, '/'); cout << "The second part is:" << c << endl; return 0; }
ignore函数
作用:跳过输入流中n个字符;
用法:cin.ignore(跳过字符数n,终止字符);
类型:- 有两个参数
cin.ignore(5,'A');
- 无参数/只有一个参数
ignore();//相当于ignore(1,EOF)
补充
以上介绍的各个成员函数,可以用cin流对象和istream类的其他流对象调用
-
输入流类(istream)常用成员函数
2016-04-15 08:46:42C++标准库里有针对外设输入操作进行处理的类——istream。而常用的cin则是istream的类对象。因此实际上我们可以重新定义新的输入流对象代替cin对输入进行操作。而我们常用的istream类成员函数有如下一些: -
一些与输入有关的istream类成员函数
2015-06-21 17:38:17除了前面介绍的用于读取数据的成员函数外,istream类还有其他在输入数据时用得着的一些成员函数。 eof 函数 eof是end of file的缩写,表示“文件结束”。从输入流读取数据,如果到达文件末尾(遇文件结束符),eof... -
自定义输入输出流(istream/ostream)
2021-02-07 19:47:44所以对于一般要自定义流式操作需要了解istream/ostream, streambuf这两个类的用途。 这里只用istream也就是输入流举例: istream定义了对设备或者buffer的操作方法,包含的方法有read,readsome, >>这些读取... -
C++中istream的使用
2018-07-09 15:56:27在项目中会经常用到读取一些配置数据,这些数据根据实际需要有可能会调整,如果将这些数据直接嵌入进...最常用到的读取配置文件的方式是使用istream类。std::istream: input stream. Input stream objects can re... -
关于istream输入流对象cin的输入函数
2020-02-23 23:28:02文章目录I/Ocin>>get()与cin.get()cin.getline()getline()gets() I/O ...cin是一个istream类的对象,它从标准输入设备(键盘)获取数据,程序中的变量通过流提取运算符>>从流中提取数... -
istream类方法 重载的抽取操作符和 cin输入
2012-01-20 00:12:23istream类方法 重载的抽取操作符和 cin输入 2010年09月04日 重载的抽取操作符 >> istream类(在头文件iostream中定义)重载了抽取操作符 >> ,使之能够识别下面的这些基本类型: signed char ...