轉載:http://blog.csdn.net/mafuli007/article/details/7271975
C++通過以下幾個類支持文件的輸入輸出
(1) ofstream:寫操作,輸出文件類;
(2) ifstream:讀操作,輸入文件類;
(3) fstream:可同時讀寫的文件類。
1. open函數:打開文件
函數原型:void open(const char*filename,int mode,int access);
參數說明:filename: 要打開的文件名
mode: 要打開文件的方式
access: 打開文件的屬性
打開文件的方式在類ios(是所有流式I/O類的基類)中定義,常用的值如下:
ios::app: 以追加的方式打開文件
ios::ate: 文件打開後定位到文件尾,ios:app就包含有此屬性
ios::binary: 以二進制方式打開文件,缺省的方式是文本方式。兩種方式的區別見前文
ios::in: 文件以輸入方式打開
ios::out: 文件以輸出方式打開
ios::nocreate: 不建立文件,所以文件不存在時打開失敗
ios::noreplace:不覆蓋文件,所以打開文件時如果文件存在失敗
ios::trunc: 如果文件存在,把文件長度設爲0
可以用“或”把以上屬性連接起來,如ios::out|ios::binary
打開文件的屬性取值是:
0:普通文件,打開訪問
1:只讀文件
2:隱含文件
4:系統文件
例如:以二進制輸入方式打開文件c:\config.sys
fstreamfile1;
file1.open("c:\\config.sys",ios::binary|ios::in,0);
ofstream file;
file.open ("example.bin", ios::out |ios::app | ios::binary);
2. close函數
函數原型:void close()
3. 二進制文件(Binary files)
在二進制文件中,使用<< 和>>,以及函數(如getline)來操作符輸入和輸出數據,沒有什麼實際意義,雖然它們是符合語法的。
文件流包括兩個爲順序讀寫數據特殊設計的成員函數:write 和 read。第一個函數 (write) 是ostream 的一個成員函數,都是被ofstream所繼承。而read 是istream 的一個成員函數,被ifstream 所繼承。類 fstream 的對象同時擁有這兩個函數。它們的原型是:
write ( char *buffer, streamsize size );
read ( char * buffer, streamsize size );
這裏 buffer 是一塊內存的地址,用來存儲或讀出數據。參數size 是一個整數值,表示要從緩存(buffer)中讀出或寫入的字符數。
- // reading binary file
- #include <iostream>
- #include <fstream.h>
- const char * filename = "example.txt";
- int main () {
- char * buffer;
- long size;
- ifstream file (filename, ios::in|ios::binary|ios::ate);
- size = file.tellg();
- file.seekg (0, ios::beg);
- buffer = new char [size];
- file.read (buffer, size);
- file.close();
- cout << "the complete file is in a buffer";
- delete[] buffer;
- return 0;
- }
寫文件舉例:
- // writing on a text file
- #include <fiostream.h>
- int main () {
- ofstream examplefile ("example.txt");
- if (examplefile.is_open()) {
- examplefile << "This is a line.\n";
- examplefile << "This is another line.\n";
- examplefile.close();
- }
- return 0;
- }
讀文件舉例
- // reading a text file
- #include <iostream.h>
- #include <fstream.h>
- #include <stdlib.h>
- int main () {
- char buffer[256];
- ifstream examplefile ("example.txt");
- if (! examplefile.is_open())
- { cout << "Error opening file"; exit (1); }
- while (! examplefile.eof() ) {
- examplefile.getline (buffer,100);
- cout << buffer << endl;
- }
- return 0;
- }
C++操作文件舉例
- #include <fstream>
- #include <iostream>
- using namespace std;
- int main()
- {
- const int SZ=100;
- char buf[SZ];
- {
- ifstream in;
- in.open("example.cpp",ios::in);
- ofstream out;
- out.open("example.out");
- int j=1;
- while(in.get(buf,SZ))
- {
- in.get();
- cout<<buf<<endl;
- out<<j++<<":"<<buf<<endl;
- }
- }
- ifstream in("example.out");
- while(in.getline(buf,SZ))
- {
- char *cp=buf;
- while(*cp!=':')
- {
- ++cp;
- }
- cp+=2;
- cout<<cp<<endl;
- }
- system("pause");
- return 0;
- }
C++中提供了移動文件指針的成員函數,從而對文件的進行隨機地讀寫。類istream針對讀指針提供3個成員函數:
tellg()//返回輸入文件讀指針的當前位置;
seekg(文件中的位置)//將輸入文件中的讀指針移動到指定位置
seekg(位移量,參照位置)//以參照位置爲基準移動若干字節
其中參照位置是枚舉值:
ios::beg//從文件開頭計算要移動的字節數
ios::cur//從文件指針的當前位置計算要移動的字節數
ios::end//從文件的末尾計算要移動的字節數
如果參照位置省略,則默認爲beg。而類ostream針對寫指針提供的3個成員函數:
tellp()//返回輸出文件寫指針的當前位置;
seekp(文件中的位置)//將輸出文件中的寫指針移動到指定位置
seekp(位移量,參照位置)//以參照位置爲基準移動若干字節
1
#include <iostream> #include <fstream> #include <cmath> using namespace std;
int main()
{
ifstream in;
in.open(url,ios::app| ios::binary); if(!in.is_open()) { cout<<"open file is failed"; return 1; }
char* buffer = new char[100];
while(!in.eof())
{
in.read(buffer,10);// 讀完10個字節到buffer中,文件指針在第10個字節;
in.seekg(1,ios::cur);// 移動一個字節,從11個字節讀取到buffer中;
}
in.close();
return 0;
}