cin.get();
cin.getline();
cin.putback();
cout.put(char);
<< endl ; 對(duì)應(yīng)getline();
//技巧:cout.put(cin.get());輸出輸入的字符
include <iostream>
include <string>
include <fstream>
include <ctime>
using namespace std;
int main()
{
ofstream fout("a.txt");
char a,b,c,d;
a = cin.get();
cin.get(b); //輸入一個(gè)字符
cin.get(c).get(d);
fout << a << endl;
fout << b << endl;
fout << c << endl;
fout << d << endl;
fout.close();
string s;
ifstream fin("a.txt");
if(!fin)
{
cout << "error" << endl;
}
while(getline(fin,s,'\n'))
{
for(int i = 0; i < 5; i++)
{
long t = time(NULL);
while(time(NULL) == t);
cout << s <<flush;
}
}
}
//==================================
include <iostream>
include <fstream>
include <string>
include <ctime>
using namespace std;
int main(int argc, char* argv[])
{
ifstream fin(argv[1]);
char ch;
/*for(;;)
{
fin.get(ch); //輸入字符到 ch中
if(!fin)
break;
time_t t = time(NULL);
while(time(NULL) == t);
cout << ch << flush; //直接從緩存區(qū)中讀取
}
fin.close();
*/
while(fin.get(ch)) //兩種一樣
{
if (!fin)
{
break;
}
time_t t = time(NULL);
while(time(NULL) == t);
cout << ch << flush;
}
fin.get();
}
//============================
//===========================
//==========================
//=========================
cin.getline(字符數(shù)組名哩治,長(zhǎng)度)//防止長(zhǎng)度溢出,確保長(zhǎng)度要比實(shí)際長(zhǎng)度要長(zhǎng)
char string1【256】
cin衬鱼。getline(string1,257)//get whole line
cin >> string 1;//stop at the 1 blank space;
getline(cin,str)//從文件中讀取
//===========================
include <iostream>
include <fstream>
using namespace std;
include <string>
int main()
{
string str;
char buf[20];
cout << "input a line" << endl;
getline(cin,str);
cout << "str = " << str << endl;
cout << "input a line(<20)" << endl;
cin.getline(buf,20);
cout << "buf = " << buf << endl;
cin.clear(); //但是如果存在cin.clear()业筏。就可以清空緩存區(qū)。鸟赫。buf沒(méi)有字節(jié)的限制
//當(dāng)讀的元素大于20蒜胖,輸入錯(cuò)誤狀態(tài)消别。就不再執(zhí)行任何輸入
if (!cin)
{
cout << "read error state" << endl;
}
int n;
cout << "input an integer" << endl;
cin >> n;
cout << "n = " << n <<endl;
}
::有cin。clear()主看buf 存儲(chǔ)
沒(méi)cin.clear()
include <iostream>
include <fstream>
using namespace std;
include <string>
int main()
{
ifstream fin("a.txt");
string str;
getline(fin,str,':'); //讀字符串翠勉,讀到: 結(jié)束
cout << "username:"<< str<< endl;
getline(fin,str,':');
cout << "password:"<< str<< endl;
getline(fin,str,':');
cout << "user id:"<< str<< endl;
getline(fin,str,':');
cout << "group id:"<< str << endl;
getline(fin,str);
cout << "info:" << str << endl;
cout << "first ch (only one)" << endl;
char no;
char ch = cin.get();
fin.clear();
char name[20];
cout << ch<< endl;//只輸出一個(gè)字符
cout << "switch ASCLL " << endl;
cout << cin.get()<< flush; //將輸入的字符轉(zhuǎn)換為ASCLL
//===================================
cin.clear();
cout <<" second ch "<<endl;
ch = cin.peek(); //peek 偷看功能(偷看下一個(gè)字符施舍么)
cout << ch <<endl;
if (ch >= '0' &&ch <= '9' )
{
cin >> no;
cout << "no = " << no <<endl;
}
else
{
cin.getline(name,20);
cout << "name = " << name<< endl;
}
//a.txt 內(nèi)容:china:123456:477:147:please into
//從文件中依次讀取讀取
}
//getline(cin,str,'\n'); 到\n結(jié)束
//cin.getline(cin,str,'\n');
//peek TK 偷看輸入流的字符妖啥,返回Ascll
//cin.putback 把取走的字符霉颠,退回到輸入符中
//putback()
include <iostream>
include <fstream>
include <string>
using namespace std;
int main()
{
char ch;
int n;
cout << "input an integer "<< endl;
cin.get(ch);//get會(huì)讀走一個(gè)字符
cin >> n;
cout << "ch = " << ch << endl;
cout << " n = " << n << endl;
cout << "input an integer "<< endl;
cin.get(ch);
cin.putback(ch);//讀走一個(gè)字符对碌,再讀回去
cin >> n;
cout << "ch = " << ch << endl;
cout << " n = " << n << endl;
//putback(ch 現(xiàn)在是換行符朽们,putback是將第一次輸入的3869\n,中的換行符輸出了出來(lái))
}