內容提要
1.建立乙個檔案輸出流物件,通過建構函式指定磁碟檔名。
2.通過檔案輸出流物件,寫入若干行文字。
3.關閉檔案輸出流物件,然後觀察磁碟檔案內容。
4.改用open函式建立檔案輸出流,並選擇適當的檔案操作方式。
5.在原內容的後面追加若干行新內容。
基本要求
能熟練運用檔案各種操作編寫程式測試並提交程式。
#include
#include
#include
using
namespace std;
class
fileoperator
//建立檔案, 並向檔案寫入資料
outfile << i <<
" ";
} outfile << endl;
outfile.
close()
;//追加寫入
outfile.
open
("d:/myfile"
; outfile << endl <<
"追加寫: "
<< endl;
for(
int i =
0; i <
100; i++
) outfile << i <<
" ";
} outfile << endl;
outfile.
close()
;//讀檔案
ifstream infile;
infile.
open
("d:/myfile"
, ios::in)
; string tmp;
while
(!infile.
eof())
infile.
close()
;}};
intmain()
五 檔案操作
字元型的檔案會先編碼然後再儲存。所以讀取這些檔案的時候就需要解碼。檔案的開啟模式有唯讀 r 只寫 w 追加 a 以及二進位制模式和二進位制加下的唯讀 rb 只寫 wb 追加 ab 唯讀 預設模式,檔案必須存在,不存在則丟擲異常 只寫 不可讀 不存在則建立 存在則清空內容 追加 不可讀 不存在則建立 ...
C 練習 五 檔案操作
include stdio.h include stdlib.h void main while c getchar n fputc c,fp1 fclose fp1 if fp2 fopen test.txt r null printf 輸出字元 n while c fgetc fp2 eof p...
python基礎(五) 檔案操作
檔案處理 1.在python中 1.開啟檔案,得到檔案控制代碼並賦值給乙個變數 f open a.txt r encoding utf 8 預設開啟模式就為r 2.通過控制代碼對檔案進行操作 data f.read 3.關閉檔案 f.close 2.f open a.txt r 過程分析 首先由應用...