一、随机读写
c++为流提供了两套设置位置指针的成员函数,为了兼容有两个位置指针的系统,但是由于Linux和Windows系统底层只有一个位置指针,所以使用其中之一即可
istream &seekg( off_type offset, ios::seekdir origin );
功能:偏移值+基础位置设置输入流的位置指针
origin:
ios::beg 文件开头
ios::end 文件末尾
ios::cur 当前位置
istream &seekg( pos_type position );
功能:根据绝对位置设置输入流的位置指针
ostream &seekg( off_type offset, ios::seekdir origin );
功能:偏移值+基础位置设置输出流的位置指针
ostream &seekg( pos_type position );
功能:根据绝对位置设置输出流的位置指针
pos_type tellg();
功能:获取输入流位置指针
pos_type tellp();
功能:获取输出流位置指针
二、C++对二进制文件进行读写//对象不要写指针 ( string name X char name[20] √)
1、创建流对象,打开文件
使用构造函数、open函数打开文件,mode参数增加ios::binary
2、读写操作
istream &read(char* buffer, streamsize num);
buffer: 存储读取到的数据的缓冲区首地址
num:缓冲区的大小