用於內存與文件之間的數據傳輸的三個類:
- ifstream類:讀文件
- ofstream類:寫文件
- fstream 類:讀、寫文件
常用的判斷錯誤的方法:
good(); 如果文件打開成功
bad(); 打開文件時發生錯誤
eof(); 到達文件尾
要執行文件的輸入輸出,須做以下幾件事:
1.在程序中包含頭文件fstream
2.建立文件流。建立文件流的過程就是定義流類的對象,例如:
ifstream in;
ofstream out;
fstream both;
分別定義了輸入流對象in;輸出流對象out,輸入輸出流對象both。
3.使用open()函數打開文件,也就是使某一文件與上面的某一流相聯繫。
open(filename.c_str(),ios::out); //若文件名不存在,則自動創建
open()函數原型如下:
void open (char* filename, int mode, int access=0);
第一個參數用於傳遞文件名;
第二個參數mode值表示文件的使用方式;
第三個參數打開文件的共享方式
filebuf::openprot; //默認的兼容共享方式(與MS_DOS文件兼容)
filebuf::sh_none; //獨佔,不共享
filebuf::sh_read; //讀共享
filebuf::sh_write; //寫共享
ios::in = 0x01,//供讀, (ifstream默認的打開方式)
ios::out = 0x02, //供寫,(ofstream默認的打開方式)
文件不存在則創建
若文件已存在則清空原內容
ios::ate = 0x04, //文件打開時,指針在文件尾部。可改變指針的位置,
常和in、out聯合使用:
1.以out方式打開文件時:
如果文件不存在,創建文件,
如果文件存在,則會刪除原來的文件
2.以in 方式打開文件時
如果文件不存在,打開失敗
ios::app = 0x08, //供寫(以追加的方式打開),文件不存在則創建,若文件已存在則在原文件內容後寫入新的內容,指針位置總在最後
ios::trunc = 0x10, //在讀寫前先將文件長度截斷爲0(默認)
ios::binary = 0x80 //二進制格式文件
4.對open結果進行檢查,檢查打開操作是否成功
fout.is_open(); //用來判斷文件打開是否成功(建立文件信息區)
if(fout.fail()); //判斷流的狀態
if(! fout.good()); //判斷流的狀態
if(! fout); //測定對象fout是否 建立/存在(fstream 類對象不能使用這種方式)
5.進行讀寫。在建立(或打開)的文件上執行所要求的輸入或輸出操作;
6.關閉文件流對象。
fout.close();
例如:
寫文件:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream ost("E:\\Documents\\test4.txt");
if(!ost)
{
return 0;
}
ost<<111<<endl;
ost.close();
}
先寫後讀:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream ost;
ost.open("E:\\Documents\\test2.txt");
ost<<212434<<endl;
ost<<123456<<endl;
ost.close();
ifstream ist;
ist.open("E:\\Documents\\test2.txt");
int a,b;
ist>>a>>b;
ist.close();
cout<<a<<endl<<b<<endl;
}
文件複製:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream ist("E:\\Documents\\copy.txt");
if(!ist) return 0;
ofstream ost("E:\\Documents\\copynew.txt");
if(!ost) return 0;
while(ist&&ist.get(ch))
{
ost.put(ch);
}
ist.close();
ost.close();
cout<<"ok!"<<endl;
}
建立學生的文本文件:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string name;
int num,score;
ofstream ost("E:\\Documents\\test5.txt");
if(!ost) return 0;
ost<<"學生成績文件:"<<endl;
while(cin>>num>>name>>score)
{
ost<<num<<" "<<name<<" "<<score<<endl;
}
ost.close();
}
學生信息操作:
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
struct student{
string name ;
int number , score ;
};
class FileOperation
{
string filename;
student record[100]; //假設文件中存儲的及記錄個數不超過100
int num;//存儲記錄的個數
public:
FileOperation();
void display();
void append();
~FileOperation();
};
FileOperation::FileOperation(){
cin>>filename;
ifstream instuf(filename.c_str(),ios::in);
num=0;
if (!instuf)
{exit(0);}
instuf >>record[num].number>>record[num].name>>record[num].score;
while(!instuf.eof())
{
num++;
instuf>>record[num].number>>record[num].name>>record[num].score;
}
instuf.close();
}
void FileOperation::display()
{
cout<<"number"<<"\t"<<"name"<<"\t"<<"score"<<endl;
for(int i=0;i<num;i++)
cout<<record[i].number<<'\t'<<record[i].name<<'\t'<<record[i].score<<'\n';
}
void FileOperation::append()
{
cin>>record[num].number>>record[num].name>>record[num].score;
num++;
}
FileOperation::~FileOperation()
{
ofstream outstuf ;
outstuf.open(filename.c_str(),ios::out);
if(!outstuf)
abort();
for(int i=0;i<num;i++)
outstuf<<record[i].number<<' '<<record[i].name<<' '<<record[i].score<<'\n';
outstuf.close() ;
}
int main()
{
FileOperation f;
f.append();
f.display();
return 0;
}