與 C 的比較 :

比較對於程式人員,方便使用 

讀檔

#include<iostream>
#include <fstream>

using namespace std;

int main(){

ifstream fin("file_A.txt");

if(!fin){    cout << "讀檔失敗" << endl;   }    // 檢查讀檔成功與否

string s;

fin>> s ;
// 或 getline(fin,s);

fin.close();   // 關閉檔案
return 0;
}

 

開檔

#include<iostream>
#include <fstream>

using namespace std;

int main(){

ofstream fout("file_B.txt");

if(!fout){    cout << "開檔失敗" << endl;   }    // 檢查開檔成功與否

fout<<"要輸出的文字";

fout.close();   // 關閉檔案
return 0;
}


arrow
arrow
    全站熱搜

    finalfrank 發表在 痞客邦 留言(3) 人氣()