C 對file的寫操作

2021-06-23 09:52:45 字數 788 閱讀 3417

對file的操作有幾個步驟,如下:

1. include 庫函式fstream; 

#include

2. 建立ofstream object;

ofstream outfile;

3. 將建立的object與檔案鏈結(開啟檔案);

outfile.open(「carinfo.txt」);

4. 對檔案進行寫操作;

outfile << fixed;

outfile.precision(2);

outfile.setf(ios_base::showpoint);

outfile << 「make and model: 「 << automobile << endl;

outfile << 「year: 「 << year << endl;

outfile << 「was asking $」 << a_price << endl;

outfile << 「now asking $」 << d_price << endl;

5. close 檔案:

outfile.close();

備註:當你開啟乙個已經存在的file時,預設情況下file內容會被清除,也就是說file之前的內容會丟失。

下面是c++裡面對file寫操作的**:

// outfile.cpp -- writing to a file

#include #include // for file i/o

int main()

C 檔案操作 File類

file類的常用靜態方法表 方 法 說 明 create 在指定路徑中建立檔案 delete 刪除指定的檔案 exists 判斷指定的檔案是否存在 open 開啟指定路徑上的檔案 openread 開啟檔案以進行讀取 openwrite 開啟檔案以進行寫入 move 將指定檔案移到新位置 copy ...

File操作 寫檔案 讀檔案 檔案追加

任務一 寫檔案 檔案追加 要求1.建立檔案,並將相關資料寫入到檔案中。2.每一次儲存資料都將資料追加到檔案末尾。public static void writeadfile context ctx,file file,int id writer.write string.valueof id wri...

基於C的流式檔案操作 FILE

這種檔案操作有乙個重要的結構 file file這個結構包含了檔案操作的基本屬性,對檔案的操作都要通過這個結構的指標來進行。接下來介紹這種操作的一些常用函式。fopen fopen的原型是 file fopen const char filename,const char mode 引數filena...