- 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( )
{
cin.ignore( 5, 'c' );
}
Type 'abcdef': abcedddsafasd
edddsafasd请按任意键继续. . .
---------------------------------------------------------------------------------------------------------
cin.peek( ) 输出字符串第一个字符
// basic_istream_peek.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
}
Type 'abcde': afdsfdf
a afdsfdf
请按任意键继续. . .
-------------------------------------------------------------------------------------------------------
cin.putback(ch)将字符ch送回字符流中
// basic_istream_putback.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main( )
{
}
asfsdf
a
asfsdf
请按任意键继续. . .
---------------------------------------------------------------------------------------------------------------
cin.read(c, count);从字符串流中读取count个字符到c数组中
// basic_istream_read.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main()
{
}
Type 'abcde': asfsdfsdfd
asf
请按任意键继续. . .
当cin.read(c,count);换为cin.get(c,10)时,
Type 'abcde': afdsfasfdf
afd
f
请按任意键继续. . .
----------------------------------------------------------------------------------------------------------------
file.seekg( 2);当刚打开文件时,指针默认指向第一个字符,2代表移动两个字符,(此时指向第三个)
file.seekg( 2, ios_base::beg);强制指针指向第一个字符,。。。。。。。也有
file.seekg( 2, ios_base::end)强制指针指向字符串的结尾,最后一字符的后一位
#include<iostream>
#include <fstream>
int main ( )
{
}
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