C++ read(),write() ,seekg(),tellg()


istream &seekg(streamoff offset,seek_dir origin);

pos_type  tellg() 返回流置针所在的位置,返回值为整数

从文件origin位置开始移动offset个字节

读写数据块

要读写二进制数据块,使用成员函数read()和write()成员函数,它们原型如下:

read(unsigned char *buf,int num);

write(const unsigned char *buf,int num);

read()从文件中读取 num 个字符到 buf 指向的缓存中,如果在还未读入 num 个字符时就到了文件尾,可以用成员函数 int gcount()来取得实际读取的字符数;而 write() 从buf 指向的缓存写 num 个字符到文件中,值得注意的是缓存的类型是 unsigned char *,有时可能需要类型转换。

相关内容