C++文件操作函數詳解

轉載: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)中讀出或寫入的字符數。

  1. // reading binary file  
  2.     #include <iostream>  
  3.     #include <fstream.h>  
  4.     const char * filename = "example.txt";  
  5.     int main () {  
  6.         char * buffer;  
  7.         long size;  
  8.         ifstream file (filename, ios::in|ios::binary|ios::ate);  
  9.         size = file.tellg();  
  10.         file.seekg (0, ios::beg);  
  11.         buffer = new char [size];  
  12.         file.read (buffer, size);  
  13.         file.close();  
  14.         cout << "the complete file is in a buffer";  
  15.         delete[] buffer;  
  16.         return 0;  
  17.     }  

寫文件舉例:

  1. // writing on a text file  
  2. #include <fiostream.h>  
  3.   
  4. int main () {  
  5.     ofstream examplefile ("example.txt");  
  6.     if (examplefile.is_open()) {  
  7.         examplefile << "This is a line.\n";  
  8.         examplefile << "This is another line.\n";  
  9.         examplefile.close();  
  10.     }  
  11.     return 0;  
  12. }  

讀文件舉例

   
  1. // reading a text file  
  2.    #include <iostream.h>  
  3.    #include <fstream.h>  
  4.    #include <stdlib.h>  
  5.      
  6.    int main () {  
  7.        char buffer[256];  
  8.        ifstream examplefile ("example.txt");  
  9.        if (! examplefile.is_open())  
  10.        { cout << "Error opening file"; exit (1); }  
  11.        while (! examplefile.eof() ) {  
  12.            examplefile.getline (buffer,100);  
  13.            cout << buffer << endl;  
  14.        }  
  15.        return 0;  
  16.    }  
  

C++操作文件舉例

  1. #include <fstream>  
  2. #include <iostream>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.         const int SZ=100;  
  7.         char buf[SZ];  
  8.         {  
  9.                ifstream in;  
  10.                in.open("example.cpp",ios::in);  
  11.                ofstream out;  
  12.                out.open("example.out");  
  13.                int j=1;  
  14.                while(in.get(buf,SZ))  
  15.                {  
  16.                        in.get();  
  17.                        cout<<buf<<endl;  
  18.                        out<<j++<<":"<<buf<<endl;  
  19.                }  
  20.         }  
  21.         ifstream in("example.out");  
  22.         while(in.getline(buf,SZ))  
  23.         {  
  24.                char *cp=buf;  
  25.                while(*cp!=':')  
  26.                {  
  27.                        ++cp;  
  28.                }  
  29.                cp+=2;  
  30.                cout<<cp<<endl;  
  31.         }  
  32.         system("pause");  
  33.         return 0;  
  34. }  

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;
}
發佈了13 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章