cin相关函数
2014-07-30 11:08
190 查看
basic_istream::basic_istream
basic_istream::gcount
basic_istream::get
basic_istream::getline
basic_istream::ignore
basic_istream::peek
basic_istream::putback
basic_istream::read
basic_istream::_Read_s
basic_istream::readsome
basic_istream::_Readsome_s
basic_istream::seekg
basic_istream::sentry
basic_istream::sync
basic_istream::tellg
basic_istream::unget
-----------------------------------------------------------------------------------------------------------------get()
cin.get( &c[0],5);输入4个字符,遇到第五个字符时停止
此时:cin.gcount()的值也为4
get函数还有:
cin.get(ch);
cin.get(c,7,'A');获取6个字符,如在此六个字符中有A的话,停止,A不获取,仍在流中
----------------------------------------------------------------------------------------------------------------getline(c,7,‘A’)
从输入流中获得6字符,或在此6个字符中有A的话,遇到A 就停止,把A删除
---------------------------------------------------------------------------------------------------------------cin.ignore( 5, 'c');输如一字符串如在前五个字符中有‘c’则去掉c前字符串(包括c),如没有则去掉前五个字符
// basic_istream_ignore.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
char chararray[100];
cout<< "Type 'abcdef': ";
cin.ignore( 5, 'c' );
cin>> chararray;
cout<< chararray;
}
Type 'abcdef': abcedddsafasd
edddsafasd请按任意键继续. . .
---------------------------------------------------------------------------------------------------------cin.peek( ) 输出字符串第一个字符
// basic_istream_peek.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
char c[10], c2;
cout<< "Type 'abcde': ";
c2 = cin.peek();//必须放在前面
cin.getline( c, 9 );
cout<< c2<< " "<< c <<endl;
}
Type 'abcde': afdsfdf
a afdsfdf
请按任意键继续. . .
-------------------------------------------------------------------------------------------------------cin.putback(ch)将字符ch送回字符流中
// basic_istream_putback.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
char c[10], c2;
c2 = cin.get();
cin.putback( c2 );
cin.getline(&c[0], 9 );
cout<<c2<<endl;//虽然字符被返回了,这里的字符还是能输出的
cout<< c <<endl;
}
asfsdf
a
asfsdf
请按任意键继续. . .
---------------------------------------------------------------------------------------------------------------cin.read(c, count);从字符串流中读取count个字符到c数组中
// basic_istream_read.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main()
{
charc[10];
int count =3;
cout << "Type 'abcde': ";
// cin.get(c,10);
cin.read(c,count);
c[count] = 0;//以此作为字符串结束符,没有的话,输出会有异
cout << c<< endl;
cout<< c[count+1]<< endl;//此时c[count+1]是没有字符的
}
Type 'abcde': asfsdfsdfd
asf //说明数组中字符串为asf0
请按任意键继续. . .
当cin.read(c,count);换为cin.get(c,10)时,
Type 'abcde': afdsfasfdf
afd
f //说明数组中字符串为afd0fasfdf
请按任意键继续. . .
----------------------------------------------------------------------------------------------------------------file.seekg( 2);当刚打开文件时,指针默认指向第一个字符,2代表移动两个字符,(此时指向第三个)
file.seekg( 2, ios_base::beg);强制指针指向第一个字符,。。。。。。。也有
file.seekg( 2, ios_base::end)强制指针指向字符串的结尾,最后一字符的后一位
#include<iostream>
#include <fstream>
int main ( )
{
using namespace std;
ifstream file;
char c, c1;
file.open("c:\\basic_istream_seekg.txt" );
file.seekg(2); // chars toskip
file>> c;
cout<< c <<endl;
file.seekg( 0,ios_base::beg );
file>> c;
cout<< c <<endl;
file.seekg( -1,ios_base::end );
file>> c1;
cout<< c1<< endl;
}
2
0
9
Press any key to continue
--------------------------------------------------------------------------------------------------------------------streamoff i;i=file.tellg();返回的是文件内指针,整形
basic_istream::gcount
basic_istream::get
basic_istream::getline
basic_istream::ignore
basic_istream::peek
basic_istream::putback
basic_istream::read
basic_istream::_Read_s
basic_istream::readsome
basic_istream::_Readsome_s
basic_istream::seekg
basic_istream::sentry
basic_istream::sync
basic_istream::tellg
basic_istream::unget
-----------------------------------------------------------------------------------------------------------------get()
cin.get( &c[0],5);输入4个字符,遇到第五个字符时停止
此时:cin.gcount()的值也为4
get函数还有:
cin.get(ch);
cin.get(c,7,'A');获取6个字符,如在此六个字符中有A的话,停止,A不获取,仍在流中
----------------------------------------------------------------------------------------------------------------getline(c,7,‘A’)
从输入流中获得6字符,或在此6个字符中有A的话,遇到A 就停止,把A删除
---------------------------------------------------------------------------------------------------------------cin.ignore( 5, 'c');输如一字符串如在前五个字符中有‘c’则去掉c前字符串(包括c),如没有则去掉前五个字符
// basic_istream_ignore.cpp
// compile with: /EHsc
#include <iostream>
int main( )
{
using namespace std;
char chararray[100];
cout<< "Type 'abcdef': ";
cin.ignore( 5, 'c' );
cin>> chararray;
cout<< chararray;
}
Type 'abcdef': abcedddsafasd
edddsafasd请按任意键继续. . .
---------------------------------------------------------------------------------------------------------cin.peek( ) 输出字符串第一个字符
// basic_istream_peek.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
char c[10], c2;
cout<< "Type 'abcde': ";
c2 = cin.peek();//必须放在前面
cin.getline( c, 9 );
cout<< c2<< " "<< c <<endl;
}
Type 'abcde': afdsfdf
a afdsfdf
请按任意键继续. . .
-------------------------------------------------------------------------------------------------------cin.putback(ch)将字符ch送回字符流中
// basic_istream_putback.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
char c[10], c2;
c2 = cin.get();
cin.putback( c2 );
cin.getline(&c[0], 9 );
cout<<c2<<endl;//虽然字符被返回了,这里的字符还是能输出的
cout<< c <<endl;
}
asfsdf
a
asfsdf
请按任意键继续. . .
---------------------------------------------------------------------------------------------------------------cin.read(c, count);从字符串流中读取count个字符到c数组中
// basic_istream_read.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main()
{
charc[10];
int count =3;
cout << "Type 'abcde': ";
// cin.get(c,10);
cin.read(c,count);
c[count] = 0;//以此作为字符串结束符,没有的话,输出会有异
cout << c<< endl;
cout<< c[count+1]<< endl;//此时c[count+1]是没有字符的
}
Type 'abcde': asfsdfsdfd
asf //说明数组中字符串为asf0
请按任意键继续. . .
当cin.read(c,count);换为cin.get(c,10)时,
Type 'abcde': afdsfasfdf
afd
f //说明数组中字符串为afd0fasfdf
请按任意键继续. . .
----------------------------------------------------------------------------------------------------------------file.seekg( 2);当刚打开文件时,指针默认指向第一个字符,2代表移动两个字符,(此时指向第三个)
file.seekg( 2, ios_base::beg);强制指针指向第一个字符,。。。。。。。也有
file.seekg( 2, ios_base::end)强制指针指向字符串的结尾,最后一字符的后一位
#include<iostream>
#include <fstream>
int main ( )
{
using namespace std;
ifstream file;
char c, c1;
file.open("c:\\basic_istream_seekg.txt" );
file.seekg(2); // chars toskip
file>> c;
cout<< c <<endl;
file.seekg( 0,ios_base::beg );
file>> c;
cout<< c <<endl;
file.seekg( -1,ios_base::end );
file>> c1;
cout<< c1<< endl;
}
2
0
9
Press any key to continue
--------------------------------------------------------------------------------------------------------------------streamoff i;i=file.tellg();返回的是文件内指针,整形
// basic_istream_tellg.cpp // compile with: /EHsc #include <iostream> #include <fstream> int main() { using namespace std; ifstream file; char c; streamoff i; file.open("basic_istream_tellg.txt"); i = file.tellg(); file >> c; cout << c << " " << i << endl; i = file.tellg(); file >> c; cout << c << " " << i << endl; }
------------------------------------------------------------------
cin.unget();将最近获取的一个字符返回到流中
#include <iostream> using namespace std;
int main( ) { char c[10], c2,c1; cout << "Type 'abc': "; c1 = cin.get( ); c2 = cin.get( ); cin.unget( ); cin.getline( c, 9 ); cout << "the c1 is "<<c1 << endl; cout << "the c2 is "<<c2 << endl; cout << "the c is "<<c << endl; }
Type 'abc': asgsgasg the c1 is a the c2 is s the c is sgsgasg //s被返回到字符流中,被c数组获得 Press any key to continue
相关文章推荐
- C++ 输入流 cin/ ifstream/ istringstream 及几个相关函数在遇到文件结束符EOF时的行为
- C++的输入cin函数的相关用法1
- STL中流相关的输入输出符和get函数彻底总结:cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- 根据类名和函数名字符串调用相关函数
- 详解Variant 的相关函数
- Variant的相关函数
- BCB中与路径文件名相关的几个函数!
- Openssl中大数以及RSA相关函数介绍
- 与中文输入相关的函数
- send函数特点及相关问题
- 中国移动短信网关CMPP3.0 C#源代码:事件定义及相关工具函数
- IL代码底层运行机制之函数相关
- 发布三个ajax相关的函数,包括无刷新提交表单等
- ShellExecute函数相关用法
- 处理了与日期相关的一些函数
- openssl之RSA相关函数介绍
- 数据操作相关函数笔记
- T-SQL: 17 个与日期时间相关的自定义函数(UDF),周日作为周的最后一天,均不受 @@DateFirst、语言版本影响!
- DataGrid DropDownList 相关处理函数
- 调用API的SDK相关知识:实现回调函数.