c 讀取 寫入 文字檔案

2021-10-09 05:40:13 字數 1244 閱讀 7822

#include  #include // 讀寫檔案的標頭檔案

#include using namespace std;

/* 1 文字檔案 寫檔案

1 包含標頭檔案

#include 2 建立流物件

ofstream ofs;

3 指定路徑和開啟方式

ofs.open(路徑, 開啟方式);

開啟方式:

ios::in 讀檔案開啟

ios::out 寫檔案開啟

ios::ate 從檔案尾開啟

ios::trunc 如果已經有檔案 先刪除在撞見

ios::binary 二進位制方式

4 寫內容

ofs << "寫點資料" << endl;

5 關閉檔案

ofs.close();

*/void write()

/*2 文字檔案 讀檔案

1 包含標頭檔案

#include 2 建立流物件

ifstream ifs;

3 指定路徑和開啟方式

ifs.open(路徑, 開啟方式);

開啟方式:

ios::in 讀檔案開啟

ios::out 寫檔案開啟

ios::ate 從檔案尾開啟

ios::trunc 如果已經有檔案 先刪除在撞見

ios::binary 二進位制方式

4 讀取 四種方式

ifs << "寫點資料" << endl;

5 關閉檔案

ifs.close();

*/void read()

// 4 讀資料 四種方式

// 第一種方式

//char buf[1024] = ;

//while (ifs >> buf)

// 第二種

//char buf[1024];

//while (ifs.getline(buf, sizeof(buf)))

// 第三種

//string buf;

//while (getline(ifs, buf))

// 第四種 不推薦用

char c;

while ((c=ifs.get()) != eof)

// 5 關閉流

ifs.close();

}int main()

C 讀取 文字檔案 寫入textbox

microsoft.win32.openfiledialog dialog new microsoft.win32.openfiledialog dialog.filter 文字檔案 txt if dialog.showdialog true 判斷檔案開啟 string path1 dialog.f...

讀取和寫入文字檔案

read a text file 的這篇文章部分描述如何使用streamreader類來讀取文字的檔案。write a text file example 1 和 write a text file example 2 在各節說明了如何使用streamwriter類來向檔案寫入文字。讀取文字檔案 若...

c 文字檔案寫入

private void 跟蹤記錄 string 輸出內容 private void 修改日記 string 輸出內容 filestream 建立只寫檔案 檔案.openwrite 建立只寫檔案.close streamwriter 寫入 new streamwriter 儲存檔名,true 以可以...