C++檔案的輸出入(C++ File I/O)

C++理負責檔案輸出入的類別有 ifstream (檔案輸入)ofstream (檔案輸出) 以及 fstream (檔案輸出入)請注意所謂檔案的 IO (輸入 \ 輸出),是以程 式的角度而言因此檔案開啟為輸入狀態時,表示程式將由檔案讀入資(in),而 不是指將資料輸入檔案。檔案開啟為輸出狀態時,表示程式將輸出資料到檔案。為了避免混淆,我們將資料由檔案輸入到程式稱之為讀取,將資料由檔案輸出到程式稱之為寫入

˜fstream類別的使用

   fstream是一個由C++提供的類別可以用 於將資料寫入檔案或讀取檔案資料欲使用fstream類別執行檔案的 IO必須先 宣告一個 fstream類別物件。 方式如下:

 fstream  file ;    //宣 告一個fstream物 件

然後利用fstream所提供的open成員函數,開啟一個檔案。傳入open函數的參 數有兩個,分別為 :欲開啟檔案名稱 、開啟該檔案的模式參數。方式如下:

file.open(“Reader.txt”,ios::in) ;    //在 讀取模式下開啟Reader.txt

若傳入的參數超過兩個可用 | 分開

file.open(“Reader.txt”,ios::in | :ios::binary) ;    //IO以及二進位制模式下開啟Reader.txt

下 表是open函數的模式參數 :

模式參數

用途

ios::in

檔案開啟為讀取(輸入)狀態

ios::out

檔案開啟為寫入(輸出)狀態

ios::ate

從檔案結尾讀取(輸入)及寫入(輸 出)資料

ios::app

從檔案結尾寫入(輸出)資料

ios::trunc

如果檔案存在,就清除檔案 內容的資料

ios::nocreat

若開啟檔案時,檔案不存 在,則產生錯誤

ios::noreplace

若開啟檔案時,檔案存在, 且ateapp為被設定,則產生錯誤

ios::binary

以二進位模式開啟檔案,預 設的檔案模式為文字模式

 

常 用的檔案處理函數:

函數

說明

open(str,mode)

mode模式開啟名為str的檔案

close( )

關閉檔案

is_open( )

檢查檔案是否為開啟狀態, 若是則傳回true,否則傳回flase

write(str,size)

str陣列中size個字元寫入到檔案中

read(str,size)

從檔案中讀取資料至檔案結 尾為止設定給str陣列,但至多不超過size個 字元

 

範 例一: 檔案的操作範例

/*範例檔名:file_io.cpp*/

#include <fstream.h>       //載入fstream標頭檔

#define size 10

void main()

{

        fstream file;      //宣告fstream物件

        char str[size] = "string", str1[size];

 

        file.open("Reader.txt", ios::out | ios::trunc);

        //開啟檔案為輸出狀態,若檔案已存在則清除檔案內容重新寫入

        file.write(str, size);   //str寫入檔案

        file.close();       //關閉檔案

 

        file.open("Reader.txt", ios::in);        //開啟檔案為輸出狀態

       

        file.read(str1, size);  //從檔案讀取內容給str1

        file.close();       //關閉檔案

       

        cout << "Reading data from file...\n" << str1 << endl;

}

執行結果

Reading data from file...

string

 

˜檔案狀態偵 測

  進行檔案IO時,需要偵測的狀態有三 類,一為資料流錯誤,二為檔案開啟得錯誤,三為是否到達檔案結尾。

 

下面程式片段將示範開啟檔案後的錯誤檢 查:

file.open("Reader.txt", ios::in);

if(!file)     //檢查檔案是否成功開啟,如果!file為真,表示無法開啟檔案

        {

                cerr << "Can't open file!\n";

                exit(1);     //在不正常情形下,中斷程式的執行

        }

    else

        cout<<”File open successfully!\n”;

 

 

下面兩程式片段則用於檢查程式是否讀取 到檔案結尾

while(file>>str)  //若到達檔案 結尾則”file>>str”將傳回0, 跳出while回圈

{ cout<<str<<endl; }

 

while(!file.eof( ))   //若到達檔案結尾則”file>>str”將 傳回0, 跳出while回圈

{

   file>>str;

   cout<<str<<endl;

}

 

範例二:將資料輸岀至檔案

/*範例檔名:file_out.*/

#include <fstream.h>

#include <stdlib.h>  //使用exit須載入stdlib標頭檔

void main()                      //主程式開始

{

        fstream file;

        char *str[4] = {"Mary","John","Judy","Joe"};  //宣告字串指標陣列

        int id[4] = {100,200,300,400};

        file.open("Reader.txt", ios::out);      //開啟檔案

        if(!file)     //檢查檔案是否成功開啟

        {

                cerr << "Can't open file!\n";

                exit(1);     //在不正常情形下,中斷程式的執行

        }

        for(int i = 0; i < 4; i++)    {

                file << id[i] << " " << str[i] << "\n";

        }      //將資料輸出至檔案

}      //主程式結束

Reader.txt 檔案內容

100 Mary

200 John

300 Judy

400 Joe

輸出結果

100 Mary

200 John

300 Judy

400 Joe

範例三:由檔案讀取資料

/*範例檔名:file_inp.cpp*/

#include <fstream.h>

#include <stdlib.h>

#include <iomanip.h>

void main()                      //主程式開始

{

        fstream file;

        char str[8];

        int id;

        file.open("Reader.txt", ios::in);        //將檔案開啟為輸入狀態

        if(!file)     //檢查檔案是否成功開啟

        {

                cerr << "Can't open file!\n";

                exit(1);     //在不正常情形下,中斷程式的執行

        }

        cout << setw(4) << setiosflags(ios::right) << "ID"

                        << setw(8) << setiosflags(ios::right) << "Name" << endl;

        cout << "-------------------\n";

        while(file >> id >> str)     //讀取記錄,若讀取至檔案結尾則傳回0

                        cout << setw(4) << setiosflags(ios::right) << id

                                        << setw(8) << setiosflags(ios::right) << str << endl;

                //從檔案讀取資料

}      //主程式結束

Reader.txt 檔案內容

100 Mary

200 John

300 Judy

400 Joe

輸出結果

ID   Name

--------------------

100  Mary

200  John

300  Judy

400  Joe