cin相关函数

来源:互联网 发布:淘宝网规则 编辑:程序博客网 时间:2024/06/01 07:34
  • 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                                          //说明数组中字符串为asf 

请按任意键继续. . .

 

当cin.read(c,count);换为cin.get(c,10)时

Type 'abcde': afdsfasfdf
afd
                                                //说明数组中字符串为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': asgsgasgthe c1 is athe c2 is sthe c is sgsgasg          //s被返回到字符流中,被c数组获得Press any key to continue
0 0