與 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;
}

文章標籤
全站熱搜
創作者介紹
創作者 finalfrank 的頭像
finalfrank

Frank's 資訊科技潮流站

finalfrank 發表在 痞客邦 留言(4) 人氣(36,699)