c++ fstream, ifstream, ostream
来源:互联网 发布:js for循环给数组赋值 编辑:程序博客网 时间:2024/06/17 06:51
namespace: std
ifstream – 从已有的文件读
ofstream – 向文件写内容
fstream - 打开文件供读写
成员函数open()实现打开文件的操作,从而将数据流和文件进行关联,通过ofstream,ifstream,fstream对象进行对文件的读写操作
void open ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out ); void open(const wchar_t *_Filename, ios_base::openmode mode= ios_base::in | ios_base::out, int prot = ios_base::_Openprot);
参数: filename 操作文件名
mode 打开文件的方式
prot 打开文件的属性
打开文件的方式在iOS类(所以流式I/O的基类)中定义,有如下几种方式:
打开文件的属性同样在ios类中也有定义:
很多程序中,可能会碰到ofstream out(“Hello.txt”), ifstream in(“…”),fstream foi(“…”)这样的的使用,并没有显式的去调用open()函数就进行文件的操作,直接调用了其默认的打开方式,因为在stream类的构造函数中调用了open()函数,并拥有同样的构造函数,所以在这里可以直接使用流对象进行文件的操作;
打开文件时一定要检查文件是否已经打开
不操作文件一定关闭文件
getline(infile,s)将文件写入string中
#include<iostream>#include<fstream>#include<string>using namespace std;int main(){ string filename = "1.txt"; fstream infile(filename, ios::in); if (!infile) { throw runtime_error("file cannot open"); return -1; } else { string s; while (!infile.eof()) { getline(infile, s); cout << s << endl; } } infile.close(); return 0;}
#include<iostream>#include<fstream>#include<string>using namespace std;int main(){ string filename = "1.txt"; ofstream outfile; outfile.open(filename, ios::out | ios::app); if (!outfile) { throw runtime_error("file cannot open"); } else { outfile << "111" << "222" << endl; outfile.close(); }}
状态标志符的验证(Verification of state flags)
除了eof()以外,还有一些验证流的状态的成员函数(所有都返回bool型返回值):
bad()
如果在读写过程中出错,返回 true 。例如:当我们要对一个不是打开为写状态的文件进行写入时,或者我们要写入的设备没有剩余空间的时候。
fail()
除了与bad() 同样的情况下会返回 true 以外,加上格式错误时也返回true ,例如当想要读入一个整数,而获得了一个字母的时候。
eof()
如果读文件到达文件末尾,返回true。
good()
这是最通用的:如果调用以上任何一个函数返回true 的话,此函数返回 false 。
要想重置以上成员函数所检查的状态标志,你可以使用成员函数clear(),没有参数。
我们可以通过使用以下成员函数来读出或配置这些指向流中读写位置的流指针:
tellg() 和 tellp()
这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get 流指针的位置 (用tellg) 或 put 流指针的位置(用tellp).
seekg() 和seekp()
这对函数分别用来改变流指针get 和put的位置。两个函数都被重载为两种不同的原型:
seekg ( pos_type position );
seekp ( pos_type position );
使用这个原型,流指针被改变为指向从文件开始计算的一个绝对位置。要求传入的参数类型与函数 tellg 和tellp 的返回值类型相同。
seekg ( off_type offset, seekdir direction );
seekp ( off_type offset, seekdir direction );
使用这个原型可以指定由参数direction决定的一个具体的指针开始计算的一个位移(offset)。它可以是:
使用这个原型可以指定由参数direction决定的一个具体的指针开始计算的一个位移(offset)。它可以是:
流指针 get 和 put 的值对文本文件(text file)和二进制文件(binary file)的计算方法都是不同的,因为文本模式的文件中某些特殊字符可能被修改。由于这个原因,建议对以文本文件模式打开的文件总是使用seekg 和 seekp的第一种原型,而且不要对tellg 或 tellp 的返回值进行修改。对二进制文件,你可以任意使用这些函数,应该不会有任何意外的行为产生。
int main () { long l,m; ifstream in(filename, ios::in|ios::binary); l = in.tellg(); in.seekg (0, ios::end); m = in.tellg(); in.close(); cout << "size of " << filename; cout << " is " << (m-l) << " bytes.\n"; return 0; }
4.二进制文件
在二进制文件中,使用<< 和>>,以及函数(如getline)来操作符输入和输出数据,没有什么实际意义,虽然它们是符合语法的。
文件流包括两个为顺序读写数据特殊设计的成员函数:write 和 read。第一个函数 (write) 是ostream 的一个成员函数,都是被ofstream所继承。而read 是istream 的一个成员函数,被ifstream 所继承。类 fstream 的对象同时拥有这两个函数。它们的原型是:
write ( char * buffer, streamsize size );
read ( char * buffer, streamsize size );
这里 buffer 是一块内存的地址,用来存储或读出数据。参数size 是一个整数值,表示要从缓存(buffer)中读出或写入的字符数。
#include<iostream>#include<fstream>using namespace std;int main(){ char *filename = "1.txt"; char *buffer; long size,m; ifstream infile(filename,ios::in | ios::binary | ios::ate); size = infile.tellg(); buffer = new char[size]; infile.seekg(0, ios::beg); infile.read(buffer, size); infile.close(); cout << buffer << endl; cout << "The complete file is in a buffer" << endl; delete[] buffer; return 0;}
- c++ fstream, ifstream, ostream
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- c++文件流基本用法(fstream, ifstream, ostream)
- iostream fstream ostream 用法
- ifstream ostream istream ostream 不能声明
- 【c/c++】C++文件读写详解(ofstream,ifstream,fstream)
- 【C++】C++文件操作详解(ifstream、ofstream、fstream)
- [c++]C++文件读写详解(ofstream,ifstream,fstream)
- C++fstream,ifstream和outstream读取文件最后一行注意
- fstream ifstream ofstream
- fstream和ifstream详细用法
- fstream ifstream ofstream的使用
- SSL协议详解
- /lib /usr/lib /usr/local/lib的简单区分
- 【Unity&C#&数组】如何创建一个存放任何类型变量的数组
- Laravel 实践之路: 数据库迁移与数据填充
- Hadoop 框架理解
- c++ fstream, ifstream, ostream
- android之改变编码格式
- MySQL · 引擎特性 · InnoDB IO子系统
- linux c之c语言符合标准的头文件和linux常用头文件
- 【JavaScript】理解面向对象
- /proc文件系统与常用进程管理命令总结
- 关于继承
- app的生命周期 和UIApplicationDelegate的代理方法的调用顺序
- JQuery过滤选择器