Delphi VJ VB VC 討論 聯繫 首頁
MS VC 參考文件

 運算子
 輸出與輸入
 流程控制
 陣列
 指標應用
 建立函數
 參數傳遞
 動態配置記憶體
 類別物件
 建構子與解建構子
 this 指標
 例外處理
 資料流
 過載運算子
 fsteram 類別

作品介紹

免費下載

廠商軟體

fstream檔案輸出入類別

類別使用。要使用fstream類來存取檔案資料,必須先建立一個fstream物件

fstream file; //宣告一個fstream物件

然後利用fstream物件的open成員函數,開啟一個檔案,其參數有兩個,一個為檔名,另一個為開啟檔案的模式參數。

file open("Readme.txt",ios::in); //在讀取模式下開啟Readme.txt 檔


opren 函數的模式參數

模式參數

作用

ios::in

檔案開啟為讀取狀態

ios::out

檔案開啟為寫入狀態

ios::ate

從檔案結尾讀取及寫入資料

ios::app

從檔案結尾寫入資料

ios::trunc

如果檔案存在就清除檔案內的資料

ios::nocreate

如果開啟檔案時,檔案不存在,就產生錯誤

ios::noreplace

如果開啟檔案時,檔案存在,且app與ate未被設定就產生錯誤

ios::binary

以二進制模式開啟檔案,預設的檔案模式為文字模式

常用的檔案處理函數

函數

說明

open(me,mode)

以mode模式開啟名為me的檔案

close( )

關閉檔案

is__open ( )

檢查檔案是否已經開啟,已開啟傳回true否則傳回false

write(me , size)

將me陣列中size個字元寫入至檔案中

read (me , size)

從檔案中讀取資料至檔案結尾為止設定給me陣列但不得超過size個字元


範例

//檔案操作
#include
<fstream.h> //載入fstream標頭檔
#define size 10
void main()
{
fstream file;
//宣告fstream物件
char str[size] = "string", str1[size];
file.open("Reader.txt", ios::out | ios::trunc);

//開啟檔案為輸出狀態,若檔案已存在則清除檔案內容重新寫入
file.write(str, size); //將str寫入檔案
file.close(); //關閉檔案
file.open("Reader.txt", ios::in); //開啟檔案為輸出狀態
file.read(str1, size); //從檔案讀取內容給str1
file.close(); //關閉檔案
cout << "Reading data from file...\n" << str1 << endl;
}

將資料輸出至檔案。

//將資料輸出至檔案
#include <fstream.h>
#include <stdlib.h> //使用exit須載入stdlib標頭檔
void main() //主程式開始
{
fstream file;

char *str[4] = {"Mary","John","Judy","Joe"}; //宣告字串指標陣列
int id[4] = {100,200,300,400};
file.open("Reader.txt", ios::out);
//開啟檔案
if(!file) //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1); //在不正常情形下,中斷程式的執行
}

for(int i = 0; i < 4; i++)
{
file << id[i] << " " << str[i] << "\n";
}
//將資料輸出至檔案
} //主程式結束

由檔案讀取資料。

//由檔案讀取資料
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
void main() //主程式開始
{
fstream file;

char str[8];
int id;
file.open("Reader.txt", ios::in); //將檔案開啟為輸入狀態

if(!file) //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1);
//在不正常情形下,中斷程式的執行
}
cout << setw(4) << setiosflags(ios::right) << "ID"
<< setw(8) << setiosflags(ios::right) << "Name" << endl;
cout << "-------------------\n";

while(file >> id >> str) //讀取記錄,若讀取至檔案結尾則傳回0
cout << setw(4) << setiosflags(ios::right) << id<< setw(8) << setiosflags(ios::right) << str << endl;
//從檔案讀取資料
} //主程式結束

二進至檔案的讀取與輸入

//二進至檔案的讀取與輸出
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
void main() //主程式開始
{
fstream file;

char str[4][8] = {"Mary","John","Judy","Joe"}; //宣告二維字元陣列
int id[4] = {100,200,300,400};
file.open("Reader.txt", ios::out | ios::binary | ios::trunc);

//開啟檔案為二進位制輸出模式
if(!file) //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1);
//在不正常情形下,中斷程式的執行
}
for(int i = 0; i < 4; i++)
{
file.write((char *) &id[i],
sizeof(int)); //將資料輸出至檔案
file.write(str[i], 8 * sizeof(char));
}

char str1[8];
int id1;
file.close();
file.open("Reader.txt", ios::in
| ios::binary); //將檔案開啟為二進位制輸入模式
if(!file) //檢查檔案是否成功開啟
{
cerr << "Can't open file!\n";
exit(1);
//在不正常情形下,中斷程式的執行
}
cout << setw(4) << setiosflags(ios::right) << "ID" << setw(8) << setiosflags(ios::right) << "Name" << endl;
cout << "-------------------\n";
file.read((
char*) &id1, sizeof(int));
file.read(str1, 8 *
sizeof(char));
while(!file.eof()) //讀取記錄,若讀取至檔案結尾!file.eof()為假
{
cout << setw(4) << setiosflags(ios::right) << id1<< setw(8) << setiosflags(ios::right) << str1 << endl;
file.read((
char*) &id1, sizeof(int)); //從檔案讀取資料
file.read((char *) str1, 8 * sizeof(char));
}
}
//主程式結束


本網頁由
昱得資訊工作室製作(C) Copyright Valor-Studio since in 1993

 

本網站全部內容係屬【昱得資訊工作室】版權所有,轉載必究,
非經正式書面同意,不得將全部或部分內容,以任何方式轉載於其他網站或用於任何商業行為