C++基礎(chǔ)
IO和文件操作
- 標(biāo)準(zhǔn)輸入輸出的設(shè)備名有哪些
- 文件輸入類(lèi)的類(lèi)名是:iostream
nt g=0;
int fun()
{
return ++g;
}
int main(int argc, char **argv)
{
cout<<fun()<<fun()<<fun()<<fun()<<endl;
return 0;
}
//4321
- 輸出流注意事項(xiàng)
- 計(jì)算順序: 自右向左
- 輸出順序: 自左向右
- cout 在執(zhí)行時(shí)相當(dāng)于一個(gè)函數(shù),而即將輸出的4 個(gè)fun( )
- 函數(shù)相當(dāng)參數(shù),編譯器在函數(shù)調(diào)用時(shí)的入棧順序是從右向左的,所以在指向fun( )函數(shù)時(shí),依次從右向左執(zhí)行,執(zhí)行完fun()函數(shù)之后,cout輸出各個(gè)參數(shù)返回的值,此時(shí)又是從左至右進(jìn)行輸出,所以函數(shù)的執(zhí)行結(jié)果為:4321
- ignore使用
#include <iostream>
using namespace std;
int main()
{
string s;
cout<<"請(qǐng)輸入一串字符:";
//忽略輸入緩沖區(qū)前8個(gè)字符
//在前8個(gè)字符中存在結(jié)束字符,那么就忽略
//輸入緩沖區(qū)中,結(jié)束字符之前的字符
cin.ignore(8,' ');
cin>>s;
cout<<"strig s="<<s<<endl;
}
/*
1234567890=>90
123 456=>456
*/
#include <iostream>
using namespace std;
int main()
{
char ch;
cin.putback('a');
cout<<"請(qǐng)輸入一個(gè)ch數(shù)據(jù):";
cin>>ch;
cout<<"char ch="<<ch<<endl;
}
//char ch=a
#include <iostream>
using namespace std;
int main()
{
int i;
string s;
//獲得輸入緩存區(qū)中的第一個(gè)字符
cout<<"star"<<endl;
char ch=cin.peek();//沒(méi)有數(shù)據(jù)的時(shí)候等待數(shù)據(jù)輸入
cout<<"end"<<endl;
if((ch>='0')&&(ch<='9'))
{
cin>>i;//"123abc" i=123
cout<<"int i="<<i<<endl;
}
else
{
cin>>s;
cout<<"string s="<<s<<endl;
}
}
/*
asd=>asd
123=>123
asd123=>asd123
123asd=>123
*/
- 流操作算子
- 整數(shù)流的基數(shù):流操縱算子dec、oct、hex和setbase
- 整數(shù)通常被解釋為十進(jìn)制(基數(shù)為10)整數(shù)。如下方法可改變流中整數(shù)的基數(shù):
- 插人流操縱算子hex可設(shè)置十六進(jìn)制基數(shù)(基數(shù)為16)、
- 插人流操縱算子oct可設(shè)置八進(jìn)制基數(shù)(基數(shù)為8)、
- 插人流操縱算子dec可恢復(fù)十進(jìn)制基數(shù)。
- 用流操縱算子setbase來(lái)改變基數(shù),使用setbase或其他任何參數(shù)化的操縱算子都必須在程序中包含頭文件iomanip
- 如果不明確地改變流的基數(shù),流的基數(shù)是不變的
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i=11;
cout<<hex<<i<<" "<<dec<<14<<endl;
cout<<otc<<i<<" "<<dec<<14<<endl;
cout<<setbase(8)<<i<<endl;
}
/*
b 14
13 14
13
*/
- 浮點(diǎn)數(shù)的精度
- 設(shè)置浮點(diǎn)數(shù)精度(precision、setprecision)
- 可以用流操縱算子setprecision或成員函數(shù)percision控制小數(shù)點(diǎn)后面的位數(shù)。
- 設(shè)置了精度以后,該精度對(duì)之后所有的輸出操作都有效,直到下一次設(shè)置精度為止。
- 無(wú)參數(shù)的成員函數(shù)percision返回當(dāng)前設(shè)置的精度。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double root2=3.14159265;
for(int places=0;places<=9;places++)
{
cout<<setprecision(places)<<root2<<'\n';
}
}
- 設(shè)置域?qū)?setw、width)
- 成員函數(shù)ios.width設(shè)置當(dāng)前的域?qū)?即輸入輸出的字符數(shù))并返回以前設(shè)置的域?qū)挕?/li>
- 如果顯示數(shù)據(jù)所需的寬度比設(shè)置的域?qū)捫?,空位用填充字符填充?/li>
- 如果顯示數(shù)據(jù)所需的寬度比設(shè)置的域?qū)挻?顯示數(shù)據(jù)并不會(huì)被截?cái)?,系統(tǒng)會(huì)輸出所有位。
- 域?qū)捲O(shè)置僅對(duì)下一行流讀取或流插入操作有效,在一次操作完成之后,城寬又被置回0
- 未對(duì)所處理的輸出數(shù)據(jù)提供足夠的域?qū)挄r(shí),輸出數(shù)據(jù)將按需要的域?qū)掃M(jìn)行輸出,有可能產(chǎn)生難以閱讀的輸出結(jié)果。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int t1[5]={1,22,333,4444,55555};
int t2[5]={111,222,333,444,555};
for(int i=0;i<5;i++)
{
cout.width(5);
cout<<t1[i];
}
cout<<endl;
for(int j=0;j<5;j++)
{
cout.width(6);
cout<<t2[j];
}
cout<<endl;
}
/*
5:
1 22 333 444455555
111 222 333 444 555
6:
1 22 333 4444 55555
111 222 333 444 555
4:
1 22 333444455555
111 222 333 444 555
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char n[30];
cin.width(5);
while(cin>>n)
{
cout<<"n="<<n<<endl;
cin.width(5);
}
return 0;
}
/*
123
n=123
12345
n=1234
n=5
123456789
n=1234
n=5678
n=9
*/
- 文件操作
- ofstream: 寫(xiě)操作的文件類(lèi)(輸出,由ostream 引申而來(lái))
- ifstream: 讀操作的文件類(lèi)(輸入,由istream 引申而來(lái))
- fstream: 可同時(shí)讀寫(xiě)操作的文件類(lèi)(由iostream 引申而來(lái))
- 文件打開(kāi)
eg: ofstream ofile(const char *filename, openmode);
eg: ofstream ofile;
ofile.open(const char *filename, openmode)
- 文件打開(kāi)模式
- ios::in 輸入(讀)模式打開(kāi)文件
- ios:: out 輸出(寫(xiě))模式打開(kāi)文件
- ios::app 追加模式打開(kāi)文件
- ios::trunc 若文件已經(jīng)存在則清空文件的模式打開(kāi)文件
- ios::binary 二進(jìn)制方式打開(kāi)文件
- 混合使用
fstream file;
file.open(“example”, ios::out | ios::app | ios::binary);
- 打開(kāi)文件時(shí),沒(méi)有指定打開(kāi)模式時(shí),使用默認(rèn)的打開(kāi)方式;
ofstream: ios::out | ios::trunc
ifstream: ios::in
fstream: ios::in | ios::out
fsream file;
file.open(xxxxxxxxxxxxxxxxxx)
if(file!=NULL)
cout<<"open failed"<<endl;
file.close();
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
/*ofstream ofile("/home/sunsuhui/1612/11/11.16/ssh1");
ofile<<"pear"<<" "<<4.5;
ofile.close();*/ //打開(kāi)文件并輸入
/*ifstream ifile("/home/sunsuhui/1612/11/11.16/ssh1");
char text[20];
double price;
ifile>>text>>price;
cout<<text<<" "<<price;
ifile.close();*/ //打開(kāi)文件輸出
/*ofstream ofile("/home/sunsuhui/1612/11/11.16/ssh2",ios::out|ios::binary);
char temp[20]="hello";
ofile.write(temp,20);
ofile.close();*/
ifstream ifile("/home/sunsuhui/1612/11/11.16/ssh2",ios::in|ios::binary);
char temp[20];
ifile.read(temp,20);
cout<<temp<<endl;
ifile.close();
}
#include <iostream>
#include <iomanip>
using namespace std;
ostream& tab(ostream& output)
{
return output<<'\t';
}
int main()
{
cout<<'a'<<tab<<'b'<<'\t'<<'c'<<endl;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile("/home/sunsuhui/1612/11/11.16/1.txt");
if(NULL==ifile)
{
cout<<"打開(kāi)文件失敗"<<endl;
return -1;
}
//定位指針 -get(讀指針)
ifile.seekg(0,ios::end);
//指針位置函數(shù)-get指針(瀆指針)
cout<<"get point position:"<<ifile.tellg()<<endl;
ifile.close();
}