C 檔案操作(文字檔案)

2021-10-13 14:08:52 字數 790 閱讀 4816

程式執行時產生的資料都屬於臨時資料,程式一旦結束都會被釋放,通過檔案可以將資料持久化,c++中對檔案操作需要包含標頭檔案

檔案型別分為兩種,文字檔案:檔案以文字的ascii碼形式儲存在計算機中;二進位制檔案:檔案以文字的二進位制形式儲存在計算機中,使用者一般不能直接讀懂它們。

操作檔案的三大類:ofstream寫檔案;ifstream讀檔案;fstream讀寫檔案。

1.寫檔案

寫檔案步驟如下

檔案開啟方式

注意:檔案開啟方式可以配合使用,利用 | 操作符,例如:利用二進位制方式寫檔案 ios::binary | ios::out

2.讀檔案

讀檔案與寫檔案步驟相似,但是讀取方式相對比較多。

讀檔案步驟如下

#include using namespace std;

#include //寫檔案

void test01()

{ ofstream ofs;

ofs.open("c:/users/administrator/desktop/test.txt", ios::out); //以寫檔案的方式開啟

ofs<<"姓名:張三"<>buff)

{ cout<:在讀檔案的過程中,可以利用is_open函式判斷檔案是否開啟成功。

c 操作文字檔案

using system.io 宣告控制項 protected system.web.ui.htmlcontrols.htmltextarea txtvalue 主程式 filestream fsinfo new filestream 檔案路徑 在專案內的 filemode.open,fileacc...

C 操作文字檔案(入門)

using system using system.data using system.configuration using system.web using system.web.security using system.web.ui using system.web.ui.webcontro...

Problem B 檔案操作 文字檔案讀入

time limit 1 sec memory limit 128 mb submit 857 solved 300 submit status web board 現有100名學生的姓名 name 學號 num 英語 english 數學 math 語文 chinese 成績儲存在乙個文字檔案st...