istream
get()
-prototype:
istream& get(char &);
-example:
char a;
cin.get(a);
說明:
從串流裡拿一個字元出來,放到a裡。
-prototype:
int get();
-example:
cout<<"press any key to continue"<<endl;
int c=cin.get();
說明:
從串流裡拿一個字元出來,以interger型態存到c裡。
-prototype:
istream& get(char *str, int len,char c = '\n');
說明:
從串流中拿出指定長度的字串,放到指標指到的裡。
getline()
-prototype:
istream& getline(char *str,int len, char c='\n');
說明:
跟get()的第三種用法相同。
ignore()
-prototype:
istream& ignore(int length = 1,char c = '\n');
說明:
從串流裡拿默認長度為1的字串忽略。
ostream
setf()
cout.setf(ios::showpos|ios::dec|ios::showpoint);
width()
cout.width(5);
precision()
cout.precision(4);
雖然也有:
cout<<setw(5)<<87<<endl;
或
cout<<fixed<<setprecision(7)<<endl;
之類的寫法,不過要另外引用<iomanip>才能使用,但上面的則是ostream裡的函式,當標頭檔寫了<iostream>時就已在內,無須另外引入。
File I/O
要引用<fstream>。
可以依據需求,使用ifstream(讀檔)或ofstream(寫檔),或者用fstream且告知讀或寫檔。
例:
ifstream File("data.txt"); // ifstream File; File.open("data.txt");
例:
ofstream File("data.txt");
例:
fstream File;
File.open("data.txt",ios::in); // 或 ios::out, ios::app
在讀寫檔前,要檢查是否正確開檔:
if(File.good())
或
if(!File())