1. C++文件讀寫詳解
1.1. 文件讀寫操作
使用方式
#include "fstream"
ofstream ofile //文件寫操作,內存寫入存儲設備
ifstream ifile //文件讀操作,存儲設備讀入內存
fstream file //文件讀寫操作
ofile.open("filename",mode) //打開文件以及打開模式
if(!ofile){ //判斷文件是否打開成功
cout << "file open error!" << endl;
}
1.1.1. 打開文件
文件操作通過成員函數open()
實現打開文件
//open
public member function
void open ( const char * filename,
ios_base::openmode mode = ios_base::in | ios_base::out );
void open(const wchar_t *_Filename,
ios_base::openmode mode= ios_base::in | ios_base::out,
int prot = ios_base::_Openprot);
- 參數:
filename
:操作文件名
mode
:打開文件的方式
prot
:打開文件的屬性 //基本很少用到,在查看資料時,發現有兩種方式
打開文件的方式在ios類(所以流式I/O的基類)中定義,有如下幾種方式:
ios::in 爲輸入(讀)而打開文件
ios::out 爲輸出(寫)而打開文件
ios::ate 初始位置:文件尾
ios::app 所有輸出附加在文件末尾
ios::trunc 如果文件已存在則先刪除該文件
ios::binary 二進制方式
這些方式是能夠進行組合使用的,以“或”運算(“|”)的方式:例如
ofstream out;
out.open("Hello.txt", ios::in|ios::out|ios::binary) //根據自己需要進行適當的選取
打開文件的屬性同樣在ios類中也有定義:
0 普通文件,打開操作
1 只讀文件
2 隱含文件
4 系統文件
對於文件的屬性也可以使用“或”運算和“+”進行組合使用,這裏就不做說明了。
很多程序中,可能會碰到ofstream out(“Hello.txt”), ifstream in("…"),fstream foi("…")這樣的的使用,並沒有顯式的去調用open()函數就進行文件的操作,直接調用了其默認的打開方式,因爲在stream類的構造函數中調用了open()函數,並擁有同樣的構造函數,所以在這裏可以直接使用流對象進行文件的操作,默認方式如下:
ofstream out("...", ios::out);
ifstream in("...", ios::in);
fstream foi("...", ios::in|ios::out);
當使用默認方式進行對文件的操作時,你可以使用成員函數is_open()
對文件是否打開進行驗證
1.1.2. 關閉文件
當文件讀寫操作完成之後,我們必須將文件關閉以使文件重新變爲可訪問的。成員函數close(),它負責將緩存中的數據排放出來並關閉文件。這個函數一旦被調用,原先的流對象就可以被用來打開其它的文件了,這個文件也就可以重新被其它的進程所訪問了。爲防止流對象被銷燬時還聯繫着打開的文件,析構函數將會自動調用關閉函數close。
1.1.3. 文本文件的讀寫
類ofstream, ifstream 和fstream 是分別從ostream, istream 和iostream 中引申而來的。這就是爲什麼 fstream 的對象可以使用其父類的成員來訪問數據。
一般來說,我們將使用這些類與同控制檯(console)交互同樣的成員函數(cin 和 cout)來進行輸入輸出。如下面的例題所示,我們使用重載的插入操作符<<:
// writing on a text file
#include <fiostream.h>
int main () {
ofstream out("out.txt");
if (out.is_open())
{
out << "This is a line.\n";
out << "This is another line.\n";
out.close();
}
return 0;
}
//結果: 在out.txt中寫入:
This is a line.
This is another line
// reading binary file
#include <iostream>
#include <fstream.h>
const char * filename = "test.txt";
int main () {
char * buffer;
long size;
ifstream in (filename, ios::in|ios::binary|ios::ate);
size = in.tellg();
in.seekg (0, ios::beg);
buffer = new char [size];
in.read (buffer, size);
in.close();
cout << "the complete file is in a buffer";
delete[] buffer;
return 0;
}
//運行結果:
The complete file is in a buffer
1.1.4. 文件的讀寫指針
ofstream fout("a1.out",ios::app); //以添加方式打開
long location = fout.tellp(); //取得寫指針位置
location = 10L;
fout.seekp(location); //將寫指針移動到第十個字節處
fout.seekp(location,ios:beg); //從頭數location
fout.seekp(location,ios::cur); //從當前位置數location
fout.seekp(location,ios::end); //從尾部數location
1.1.5. 用流迭代器讀取寫入文件
#include "vector"
#include "string"
#include "fstream"
#include "iterator"
void practice10_29() {
ifstream files;
vector<string> Str;
files.open("./hello.txt"); //打開文件
istream_iterator<string> in_iter(files); //定義文件輸出流
if (files.is_open()) //如果文件打開成功
while (!files.eof()) { //如果輸入流沒有到末尾
Str.push_back(*in_iter++); //讀取輸入流存儲到內存中
}
else {
cout << "open file failed" << endl;
}
files.close();
print(Str);
}
void test10_28() {
vector<string> Str = { "fox","red","banoria" ,"over","begin","man","hello","promise" };
ofstream ofile;
ofile.open("./hello.txt",ios::app);
auto sp = Str.begin();
ostream_iterator<string> out_iter(ofile," ");
if(ofile.is_open())
copy(Str.begin(), Str.end(), out_iter);
ofile.close();
}