C++文件操作函数详解
来源:互联网 发布:ssh默认端口号 编辑:程序博客网 时间:2024/05/22 00:47
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语言文件操作函数详解
- C语言文件操作函数详解
- C库文件操作函数详解
- c语言文件操作函数详解
- C语言文件操作函数大全详解
- c文件操作函数
- c文件操作函数
- C文件操作函数
- c文件操作函数
- C文件操作函数
- C文件操作函数
- C文件操作函数 .
- C文件操作函数
- c 文件操作函数
- C文件操作函数
- c文件操作函数
- c文件操作函数
- C语言文件操作函数(ANSI)详解(一)
- 关于char字符移位问题
- 选路逻辑问题 选路逻辑问题
- UDP协议疑难杂症全景解析
- 小量压缩简单算法
- 必然导致失败的6种行为方式
- C++文件操作函数详解
- 0 欧姆电阻
- 寻找无序数组中的第K大数
- MyEclipse快捷键收藏
- 腾讯游戏的矩阵效应
- 网站安全笔记代码
- C++ 中 vector 如何实现内存分配
- 指针和Map 指针和Map
- POJ - 1077 - 康托展开