c++的檔案流處理其實很簡單,前提是你能夠理解它。檔案流本質是利用了一個buffer中介層。有點類似標準輸出和標準輸入一樣。
c++ IO的設計保證IO效率,同時又兼顧封裝性和易用性。本文將會講述c++檔案流的用法。
有錯誤和疏漏的地方,歡迎批評指證。
需要包含的標頭檔: <fstream>
名字空間: std
也可以試用<fstream.h>
fstream提供了三個類,用來實現c++對檔案的操作。(檔案的建立,讀寫)。
ifstream -- 從已有的檔案讀
ofstream -- 向檔案寫內容
fstream - 開啟檔案供讀寫
支援的檔案類型
實際上,檔案類型可以分為兩種: 文字檔和二進位檔案.
文字檔儲存的是可讀的字元, 而二進位檔案儲存的只是位元據。利用二進位模式,你可以操作映像等檔案。用文字模式,你只能讀寫文字檔。否則會報錯。
例一: 寫檔案
聲明一個ostream變數
調用open方法,使其與一個檔案關聯
寫檔案
調用close方法.
#include <fstream.h>
void main
{
ofstream file;
file.open("file.txt");
file<<"Hello file/n"<<75;
file.close();
}
可以像試用cout一樣試用操作符<<向檔案寫內容.
Usages:
file<<"string/n";
file.put('c');
例二: 讀檔案
1. 聲明一個ifstream變數.
2. 開啟檔案.
3. 從檔案讀資料
4. 關閉檔案.
#include <fstream.h>
void main
{
ifstream file;
char output[100];
int x;
file.open("file.txt");
file>>output;
cout<<output;
file>>x;
cout<<x;
file.close();
}
同樣的,你也可以像cin一樣使用>>來操作檔案。或者是調用成員函數
Usages:
file>>char *;
file>>char;
file.get(char);
file.get(char *,int);
file.getline(char *,int sz);
file.getline(char *,int sz,char eol);
1.同樣的,你也可以使用建構函式開開啟一個檔案、你只要把檔案名稱作為建構函式的
第一個參數就可以了。
ofstream file("fl.txt");
ifstream file("fl.txt");
上面所講的ofstream和ifstream只能進行讀或是寫,而fstream則同時提供讀寫的功能。
void main()
{
fstream file;
file.open("file.ext",iso::in|ios::out)
//do an input or output here
file.close();
}
open函數的參數定義了檔案的開啟模式。總共有如下模式
屬性列表
ios::in 讀
ios::out 寫
ios::app 從檔案末尾開始寫
ios::binary 二進位模式
ios::nocreate 開啟一個檔案時,如果檔案不存在,不建立檔案。
ios::noreplace 開啟一個檔案時,如果檔案不存在,建立該檔案
ios::trunc 開啟一個檔案,然後清空內容
ios::ate 開啟一個檔案時,將位置移動到檔案尾
Notes
預設模式是文本
預設如果檔案不存在,那麼建立一個新的
多種模式可以混合,用|(按位或)
檔案的byte索引從0開始。(就像數組一樣)
我們也可以調用read函數和write函數來讀寫檔案。
檔案指標位置在c++中的用法:
ios::beg 檔案頭
ios::end 檔案尾
ios::cur 當前位置
例子:
file.seekg(
="nu0">0,ios::end);
int fl_sz = file.tellg();
file.seekg(0,ios::beg);
常用的錯誤判斷方法:
good() 如果檔案開啟成功
bad() 開啟檔案時發生錯誤
eof() 到達檔案尾
例子:
char ch;
ifstream file("kool.cpp",ios::in|ios::out);
if(file.good()) cout<<"The file has been opened without problems;
else cout<<"An Error has happend on opening the file;
while(!file.eof())
{
file>>ch;
cout<<ch;
}
以下為資料檔案讀取時的常用操作
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//輸出空行
void OutPutAnEmptyLine()
{
cout<<"/n";
}
//讀取方式: 逐詞讀取, 詞之間用空格區分
//read data from the file,
Word
By
Word
//when used in this manner, we'll get space-delimited bits of text from the file
//but all of the whitespace that separated words (including newlines) was lost.
void ReadDataFromFileWBW()
{
ifstream fin("data.txt");
string s;
while( fin >> s )
{
cout << "Read from file: " << s << endl;
}
}
//讀取方式: 逐行讀取, 將行讀入字元數組, 行之間用斷行符號換行區分
//If we were interested in preserving whitespace,
//we could read the file in
Line-
By-
Line using the I/O getline() function.
void ReadDataFromFileLBLIntoCharArray()
{
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while( fin.getline(str,LINE_LENGTH) )
{
cout << "Read from file: " << str << endl;
}
}
//讀取方式: 逐行讀取, 將行讀入字串, 行之間用斷行符號換行區分
//If you want to avoid reading into character arrays,
//you can use the C++ string getline() function to read lines into strings
void ReadDataFromFileLBLIntoString()
{
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
cout << "Read from file: " << s << endl;
}
}
//帶錯誤偵測的讀取方式
//Simply evaluating an I/O object in a boolean context will return false
//if any errors have occurred
void ReadDataWithErrChecking()
{
string filename = "dataFUNNY.txt";
ifstream fin( filename.c_str());
if( !fin )
{
cout << "Error opening " << filename << " for input" << endl;
exit(-1);
}
}
int main()
{
ReadDataFromFileWBW(); //逐詞讀入字串
OutPutAnEmptyLine(); //輸出空行
ReadDataFromFileLBLIntoCharArray(); //逐詞讀入字元數組
OutPutAnEmptyLine(); //輸出空行
ReadDataFromFileLBLIntoString(); //逐詞讀入字串
OutPutAnEmptyLine(); //輸出空行
ReadDataWithErrChecking(); //帶檢測的讀取
return 0;
}原帖地址:http://www.cnblogs.com/kevin2010_vip/archive/2010/02/03/1662853.html