C++文件操作函数详解

来源:互联网 发布:精日 知乎 编辑:程序博客网 时间:2024/05/22 09:06

转载: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)中读出或写入的字符数。

[cpp] view plain copy
print?
  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.     }  

写文件举例:

[cpp] view plain copy
print?
  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. }  

读文件举例

   
[cpp] view plain copy
print?
  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++操作文件举例

[cpp] view plain copy
print?
  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;
}
0 0
原创粉丝点击