CPlusPlusThings/practical_exercises/10_day_practice/day10/file/input/get2.cpp

45 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//【例12-2】 用函数get和getline读取数据。
#include <iostream>
using namespace std;
// cin.get(arrayname,size) 把字符输入到arrayname中长度不超过size
int main() {
// get()两个参数
// 1.输入串长<size输入串长>arraylength会自动扩张arrayname大小使能保存所有数据
// char a[10];
// cin.get(a,20);
// cout<<a<<endl;
// cout<<sizeof(a)<<endl;
// 2.输入串长<size输入串长<arraylength把串全部输入后面补\0
// char b[10];
// cin.get(b,20);
// cout<<b<<endl;//12345此时数组内数据为12345'\0
// cout<<sizeof(b)<<endl;
// 3.输入串长>size先截取size个字符若还是大于arraylength则自动扩张arrayname大小使能保存所有数据
// char c[5];
// cin.get(c,10);
// cout<<c<<endl;
// cout<<sizeof(c)<<endl;
// 4.输入串长>size先截取size个字符若小于arraylength则把截取串放入数组中最后补充\0
// char d[10];
// cin.get(d,5);
// cout<<d<<endl;
// cout<<sizeof(d)<<endl;
// get()三个参数
/*
用法cin.get(arrayname,size,s)
?把数据输入到arrayname字符数组中当到达长度size时结束或者遇到字符s时结束
注释a必须是字符数组即char
a[]l类型不可为string类型size为最大的输入长度s为控制遇到s则当前输入结束缓存区里的s不会被舍弃
*/
int i;
char e[10];
cin.get(e, 8, ',');
cout << e;
return 0;
}