1.get函数
get()函数是cin输入流对象的成员函数
三种形式:
1.不含参数——cin.get()
用来从指定的输入流中提取一个字符(包括空白字符),函数的返回值就是读入的字符。 若遇到输入流中的文件结束符,则函数值返回文件结束标志EOF(End Of File),一般以-1代表EOF,用-1而不用0或正值,是考虑到不与字符的ASCII代码混淆,但不同的C ++系统所用的EOF值有可能不同。
【例】
复制代码
1
2
3
4
5
6
7
8
9
10
11#include<bits/stdc++.h> using namespace std; int main(){ char s; while (1) { s = cin.get(); if (s == EOF) break; putchar(s); } return 0; }
复制代码
1
2
3
4
5
6
7
8
9#include<bits/stdc++.h> using namespace std; int main(){ char s; while ((s = cin.get()) != EOF) { putchar(s); } return 0; }
2.含一个参数——cin.get(s)
从输入流中读取一个字符,赋给字符变量ch。如果读取成功则函数返回true,如失败(遇文件结束符) 则函数返回false(文件结束符 为 ctrl + z)
【例】
复制代码
1
2
3
4
5
6
7
8
9#include<bits/stdc++.h> using namespace std; int main(){ char s; while (cin.get(s)) { putchar(s); } return 0; }
3.含三个参数——cin.get(char *s, int n, char c)
从输入流中读取n-1个字符,赋给指定的字符数组(或字符指针指向的数组),如果在读取n-1个字符之前遇到指定的终止字符,则提前结束读取。如果读取成功则函数返回true(真),如失败(遇文件结束符) 则函数返回false(假)。
其中,第3个参数也可以省写,此时默认为’n’。
【例】
复制代码
1
2
3
4
5
6
7
8
9#include <iostream> using namespace std; int main() { char ch[20]; cin.get(ch, 10, 'n'); cout << ch << endl; return 0; }
最后
以上就是可爱母鸡最近收集整理的关于C语言中读入或输出字符的函数1.get函数的全部内容,更多相关C语言中读入或输出字符内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复