目錄
對(duì)齊方式
‘stream’流文件讀寫
文件操作
- 流系體系
- 流:數(shù)據(jù)從一個(gè)對(duì)象到另一個(gè)對(duì)象的傳輸。
- 功能:標(biāo)準(zhǔn)輸入輸出+文件處理
分類 含義 文本流 一串ASCII子符 二進(jìn)制流 一串二進(jìn)制
- ‘ios’是抽象類
- ‘ostream’是‘cout’、‘clog’、‘cerr’的類
- ‘istream’是‘cin’的類
全局流變量 名稱 緩存 ‘cout’ 標(biāo)準(zhǔn)輸出流 帶緩存 ‘cin’ 標(biāo)準(zhǔn)輸入流 帶緩存 ‘clog’標(biāo)準(zhǔn)日志流 帶緩存 ‘cerr’ 標(biāo)準(zhǔn)錯(cuò)誤流 無緩存 - 輸出流默認(rèn)設(shè)置?
類型 進(jìn)制 寬度 對(duì)齊 填充 精度 整數(shù) 十進(jìn)制 0 右對(duì)齊 空格 1 實(shí)數(shù) 十進(jìn)制 0 右對(duì)齊 空格 6位數(shù) 字符串 - 0 右對(duì)齊 空格 字符串實(shí)際長(zhǎng)度
-
格式控制
-
格式控制成員函數(shù)
流對(duì)象.格式控制成員函數(shù)(實(shí)參)
- 1
-
預(yù)定義格式控制函數(shù)
預(yù)定義格式控制函數(shù)(實(shí)參)
- 1
-
-
流的輸出控制格式
進(jìn)制 | ‘flags()’、‘setf()’、‘unsetf()’ | ‘setiosflags()’ | ‘dec’、‘oct’、‘hex’、‘showbase’ | 能繼續(xù)使用 |
寬度 | ‘width(n)’ | ‘setw(n)’ | - | 不能連續(xù) |
對(duì)齊 | ‘flags()’、‘setf()’、‘unsetf’ | ‘setiosflags()’ | ‘right’、‘left’、‘internal’ | 能連續(xù)使用 |
填充 | ‘fill(c)’ | ‘setfill(c)’ | - | 能連續(xù)使用 |
- 流的輸出控制格式:‘dec’、‘oct’、‘hex’
- 數(shù)據(jù)輸入成員函數(shù)
- 子符輸入成員函數(shù):‘get()’
- 子符串輸入成員函數(shù):‘getline()’
- 數(shù)據(jù)輸出成員函數(shù):‘put() ’
對(duì)齊方式
flag manipulator 作用 ‘ios::left’ ‘left’ 居左 ‘ios::right’ ‘right’ 居右 ‘ios::internal’ ‘internal’ 輸出符號(hào)或進(jìn)制后填充
#include <iostream>
using namespace std;
int main(){
int n = -11;
cout.width(6);
cout.flags(ios::right);
cout << n << endl;
cout.width(6);
cout.flags(ios::left);
cout << n << endl;
cout.width(6);
cout.flags(ios::internal);
cout << n << endl;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int n = -11;
cout << setw(6) << right << n << endl;
cout << setw(6) << left << n << endl;
cout << setw(6) << internal << n << endl;
}
#include <iostream>
using namespace std;
int main(){
int n = -11;
cout.width(6); cout << left << n << endl;
cout.width(6); cout << right << n << endl;
cout.width(6); cout << internal << n << endl;
}
整數(shù)輸出格式
flag | manipulator | 作用 | 是否默認(rèn) |
---|---|---|---|
ios::dec |
dec |
十進(jìn)制 | 是 |
ios::oct |
oct |
八進(jìn)制 | 否 |
ios::hex |
hex |
十六進(jìn)制 | 否 |
ios::uppercase |
uppercase |
使用大寫輸出十六進(jìn)制 | 否 |
ios::showbase |
showbase |
輸出帶有進(jìn)制的字符 | 否 |
#include <iostream>
using namespace std;
int main(){
int n = 11;
cout.flags(ios::dec);
cout << n << endl;
cout.flags(ios::hex);
cout << n << endl;
cout.flags(ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::dec);
cout << n << endl;
cout.flags(ios::showbase|ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::hex);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::dec);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::oct);
cout << n << endl;
cout.flags(ios::showbase|ios::uppercase|ios::hex);
cout << n << endl;
}
#include <iostream>
using namespace std;
int main(){
int n = 11;
cout << dec << n << endl;
cout << hex << n << endl;
cout << oct << n << endl;
cout << showbase << dec << n << endl;
cout << oct << n << endl;
cout << hex << n << endl;
cout << uppercase << dec << n << endl;
cout << oct << n << endl;
cout << hex << n << endl;
}
浮點(diǎn)數(shù)八進(jìn)制/十六進(jìn)制不能輸出結(jié)果。?
flag | 作用 | 是否默認(rèn) |
---|---|---|
‘ios::showpoint’ | 輸出浮點(diǎn)數(shù)時(shí),必須帶小數(shù)點(diǎn) | 否 |
- 浮點(diǎn)數(shù)輸出格式?
flag 作用 是否默認(rèn) ‘ios::scientific’ 科學(xué)計(jì)數(shù)法輸出浮點(diǎn)數(shù) 否 ‘ios::fixed’ 定點(diǎn)數(shù)方式輸出實(shí)數(shù) 是
定點(diǎn)數(shù)方式比浮點(diǎn)數(shù)方式更精確
取浮點(diǎn)數(shù)精確度時(shí),設(shè)置‘ios::fixed’?
#include <iostream> // cout, std::fixed, std::scientific
using namespace std;
int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;
cout.precision(5);
cout << "default:\n";
cout << a << endl << b << endl << c << endl;
cout << "fixed:\n";
cout.flags(ios::fixed);
cout << a << endl << b << endl << c << endl;
cout << "scientific:\n";
cout.flags(ios::scientific);
cout << a << endl << b << endl << c << endl;
return 0;
}
#include <iostream> // std::cout, std::fixed, std::scientific
#include <iomanip>
using namespace std;
int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;
cout << setprecision(5)
<< "default:\n"
<< a << endl << b << endl << c << endl;
cout << "fixed:\n" << fixed
<< a << endl << b << endl << c << endl;
cout << "scientific:\n" << scientific
<< a << endl << b << endl << c << endl;
return 0;
}
#include <iostream> // std::cout, std::fixed, std::scientific
int main () {
double a = 3.1415926534;
double b = 2006.0;
double c = 1.0e-10;
std::cout.precision(5);
std::cout << "default:\n";
std::cout << a << '\n' << b << '\n' << c << '\n\n'
std::cout << "fixed:\n" << std::fixed;
std::cout << a << '\n' << b << '\n' << c << '\n\n'
std::cout << "scientific:\n" << std::scientific;
std::cout << a << '\n' << b << '\n' << c << '\n\n';
return 0;
}
- 布爾類型輸出格式
flag | manipulator | 作用 |
---|---|---|
‘ios::boolapha’ | ‘boolalpha’ | 把‘bool’值以字符串‘true’/‘false’輸出 |
- 正數(shù)或十進(jìn)制‘0’帶‘+’
flag | manipulator | 作用 |
---|---|---|
‘ios::showpos’ | ‘showpos’ | 輸出十進(jìn)制0或者正數(shù)時(shí),帶‘+’ |
#include <iostream> // std::cout, std::showpos, std::noshowpos
using namespace std;
int main () {
int p = 1;
int z = 0;
int n = -1;
cout << showpos << p << '\t' << z << '\t' << n << endl;
cout << noshowpos << p << '\t' << z << '\t' << n << endl;
return 0;
}
#include <iostream> // std::cout, std::showpos, std::noshowpos
using namespace std;
int main () {
int p = 1;
int z = 0;
int n = -1;
cout.setf(ios::showpos); cout << p << '\t' << z << '\t' << n << endl;
cout.unsetf(ios::showpos); cout << p << '\t' << z << '\t' << n << endl;
return 0;
}
字符串轉(zhuǎn)化數(shù)字
istringstream iss(str);
int n;
iss >> n;
C++11提供如下函數(shù)簡(jiǎn)化字符串轉(zhuǎn)函數(shù)?
- ‘stoi()’、‘stol()’、‘stoul’、‘stoll’、‘stoull()’、‘stof()’、‘stod()’、‘stold()’
數(shù)字轉(zhuǎn)化字符串
int n;
ostringstream oss;
oss << n;
oss.str();
‘string’字符串遍歷
string str("abcdefg");
1 .C寫法
for(size_t i = 0; i < str.size(); i++)
{
cout << str[i] << endl;
}
2 .迭代器寫法
for(string::iterator it = str.begin(); it != str.end();it++)
{
cout << str[i] << endl;
}
3 .STL ‘for_each’寫法
void print(char c){
cout << c << endl;
}
for_each(str.begin(),str.end(),print);
4 .C++11迭代器寫法
for(string::iterator it = begin(str);it != end(str); it++){
cout<< *it << endl;
}
或者
for(auto it = begin(str);it!=end(str);it++){
cout<<*it<<endl;
}
5 .C++11 for新語法寫法
for(char c : str){
cout<<c<<endl;
}
或者
for(auto c : str){
cout << c << endl;
}
6 .C++11 STL ‘for_each’與lamdba表達(dá)式
for_each(begin(str),end(str),[](char c){cout << c <<endl;});
向量遍歷
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
1 .C寫法
for(size_t i = 0; i < vec.size(); i++){
cout << vec[i] << endl;
}
2 .迭代器寫法
for(vector<int>::iterator it = vec.begin(); it != vec.end();it++){
cout<<*it<<endl;
}
3 .STL ‘for_each’寫法
void print(int n){
cout<< n << endl;
}
for_each(vec.begin(),vec.end(),print);
4 .C++迭代器寫法
for(vector<int>::iterator it = begin(vec);it != end(vec);it++){
cout << *it <<endl;
}
或者
for(auto it = begin(vec); it != end(vec);it++){
cout<<*it<<endl;
}
5 .C++11 for新語法
for(int n : vec){
cout << n << endl;
}
或者
for(auto n : vec){
cout << n << endl;
}
6 . C++11 STL ‘for_each’與lamdba表達(dá)式
for_each(begin(vec),end(vec),[](int n){cout << n <<endl;});
C++文件讀寫
文件:文件名+文件內(nèi)容(有序數(shù)據(jù)集合)
文件名:子符序列
文件數(shù)據(jù)格式:二進(jìn)制文件/文本文件
- C++文件操作流程
- 打開文件
- 讀寫文件
- 關(guān)閉文件
- 打開/關(guān)閉文件
操作 代碼 定義讀文件流對(duì)象 ‘ifstream讀文件流對(duì)象’ 定義寫文件流對(duì)象 ‘ofstream寫文件流對(duì)象’ 定義讀寫文件流對(duì)象 ‘fstream讀寫文件流對(duì)象’ 成員函數(shù)打開文件 ‘文本流對(duì)象.open(文件名,文件打開方式)’ 構(gòu)造函數(shù)打開方式 ‘文件流類 對(duì)象(文件名,文件打開方式)’ 判斷文件是否打開 ‘!文件流對(duì)象’ 關(guān)閉文件 ‘文件流對(duì)象.close(變量)’
文本文件讀寫
操作 | 代碼 |
---|---|
提取運(yùn)算符讀文件 | ‘文件流對(duì)象 >> 變量’ |
插入運(yùn)算符寫文件 | ‘文件流對(duì)象 << 變量’ |
成員函數(shù)讀文件 | ‘文件流對(duì)象.get(變量)’ |
成員函數(shù)寫文件 | ‘文件流對(duì)象.put(變量)’ |
二進(jìn)制文件讀寫
操作 | 代碼 |
---|---|
讀函數(shù) | ‘read()’ |
寫函數(shù) | ‘write()’ |
測(cè)試文件結(jié)束 | ‘eof()’ |
文件打開方式
類型 | 代碼 |
---|---|
讀 | ‘ios::in’ |
寫 | ‘ios::out’ |
添加末尾 | ‘ios::app’ |
已存在文件 | ‘ios::nocreate’ |
未打開文件 | ‘ios::noreplace’ |
二進(jìn)制 | ‘ios::binary’ |
* 文件對(duì)象指針位置函數(shù)
類型 | 代碼 |
---|---|
讀 | ‘ios::in’ |
寫 | ‘ios::out’ |
添加至行尾 | ‘ios::app’ |
已存在文件 | ‘ios::nocreate’ |
如果文件不存在則建立新文件,如果文件以存在則操作失敗 | ‘ios::noreplace’ |
二進(jìn)制 | ‘ios::binary’ |
文件對(duì)象指針位置函數(shù)
操作 | 代碼 |
---|---|
獲取讀位置 | ‘對(duì)象.tellg()’ |
獲取寫位置 | ‘對(duì)象.tallp()’ |
設(shè)置讀位置 | ‘對(duì)象.seekg()’ |
設(shè)置寫位置 | ‘對(duì)象.seekp()’ |
如果文件是以‘ios::app’文本追加方式打開,指針位置默認(rèn)在文件結(jié)束,其他情況默認(rèn)在文件開頭。?
文件對(duì)象狀態(tài)符
操作 | 代碼 |
---|---|
判斷文件對(duì)象狀態(tài)是否正常 | ‘對(duì)象.good()’ |
重置文件對(duì)象狀態(tài) | ‘對(duì)象.clear()’ |
流式文件類型
1. ‘stream’流文件
2. 文件指針‘FILE*’
‘stream’流文件讀寫
ofstream fout(文件路徑);
fin >> 變量;
fout.close();
- ‘ifstream’文件讀
-
ifstream fin(文件路徑); fin >> 變量; fin.close();
- ‘ofstream’文件寫
‘fstream’文件–先寫后讀
fstream fs(文件路徑);
if(fs){
fs << 變量;
fs.seekp(ios::beg);
fs >> 變量;
fs.close();
}
- ‘fstream’文件–先讀后寫
fstream fs(文件路徑)
if(!fs){
fs >> 變量;
fs.clear();
fs << 變量;
}
文件指針‘FILE’讀寫
- ‘FILE’文件指針讀
FILE* fp = fopen(文件路徑,"r");
fscanf( "hello", fp);
fclose(fp);
對(duì)象的序列化與反序列化
序列化:把對(duì)象轉(zhuǎn)化成文本/二進(jìn)制
反序列化:把文本/二進(jìn)制轉(zhuǎn)化成對(duì)象
文件重定向
freopen(文件路徑,操作,標(biāo)準(zhǔn)IO);
操作:讀(‘w’) 寫(‘r’)
- 重定向?qū)?/li>
freopen("out.txt","w",stdout);
cout << "out data" <<endl;
- 重定向讀
freopen("in.txt","r",stdin);
string str;
cin >> str;
幾種常見的文件讀取方式對(duì)比(效率自上而下,第一個(gè)效率最高)
1. ‘ifstream’ + ‘fin’
2. ‘freopen’ + ‘cin’ + ‘sync_with_stdio(false)’
3. ‘FILE* ’ + ‘fscanf’
4. ‘freopen’ + ‘scanf’
5. ‘freopen’ + ‘cin’?文章來源:http://www.zghlxwxcb.cn/news/detail-424147.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-424147.html
到了這里,關(guān)于C++語言基礎(chǔ)——文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!