C语言的seekg函数,c++ fstream中seekg()和seekp()的用法

先说一下C语言中fseek()的功能:

函数原型:int fseek(FILE *fp, LONG offset, int origin)

参数含义:fp 文件指针 offset 相对于origin规定的偏移位置量 origin 指针移动的起始位置,可设置为以下三种情况: SEEK_SET 文件开始位置 SEEK_CUR 文件当前位置 SEEK_END 文件结束位置

C++中seep()和seekg()函数功能

seekp:设置输出文件流的文件流指针位置

seekg:设置输入文件流的文件流指针位置

函数原型:

ostream& seekp( streampos pos );

ostream& seekp( streamoff off, ios::seek_dir dir );

istream& seekg( streampos pos );

istream& seekg( streamoff off, ios::seek_dir dir );

函数参数

pos:新的文件流指针位置值

off:需要偏移的值

dir:搜索的起始位置

dir参数用于对文件流指针的定位操作上,代表搜索的起始位置

在ios中定义的枚举类型:

enum seek_dir {beg, cur, end};

每个枚举常量的含义:

ios::beg:文件流的起始位置

ios::cur:文件流的当前位置

ios::end:文件流的结束位置

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)
C++,`seekp`和`seekg`是用来设置输出流和输入流的读写指针位置的函数。 `seekp`和`seekg`都需要两个参数: 1. 第一个参数是偏移量,表示要移动的字节数。 2. 第二个参数是一个标志,用于指定移动位置的基准点。可以使用`ios::beg`表示从文件或缓冲区开头开始,使用`ios::cur`表示当前位置,使用`ios::end`表示从文件或缓冲区末尾开始。 下面是`seekp`和`seekg`的使用示例: ```cpp #include <iostream> #include <fstream> using namespace std; int main() { // 创建一个文件输出流 ofstream out("example.txt"); // 向文件写入一些数据 out << "Hello, World!" << endl; // 设置输出流的写指针到文件开头 out.seekp(0, ios::beg); // 向文件写入另外一些数据 out << "This is a test." << endl; // 关闭输出流 out.close(); // 创建一个文件输入流 ifstream in("example.txt"); // 设置输入流的读指针到文件末尾 in.seekg(0, ios::end); // 获取文件大小 int size = in.tellg(); // 关闭输入流 in.close(); // 输出文件大小 cout << "File size: " << size << " bytes." << endl; return 0; } ``` 在上面的示例,我们创建了一个文件输出流,并向文件写入一些数据。然后,我们使用`seekp`将输出流的写指针设置到文件开头的位置,接着向文件写入另外一些数据。最后,我们关闭输出流。 接下来,我们创建了一个文件输入流,并使用`seekg`将输入流的读指针设置到文件末尾的位置。然后,我们使用`tellg`函数获取文件大小,并关闭输入流。最后,我们输出文件大小。 需要注意的是,`tellg`函数返回的是当前读指针的位置,如果我们没有使用`seekg`函数设置读指针的位置,那么`tellg`返回的值就是当前读指针相对于文件开头的偏移量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值