首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::fseek

Defined in header <cstdio>

?

?

int fseek( std::FILE* stream, long offset, int origin );

?

?

设置文件流的文件位置指示符。stream...

如果stream是以二进制模式打开的,新位置正好是offset从文件开头测量的字节数originSEEK_SET,则从当前文件位置返回originSEEK_CUR,并且从文件的末尾开始,如果originSEEK_END不需要二进制流来支持SEEK_END,特别是如果输出额外的空字节。

如果stream在文本模式下打开,则为offset为零%28,与任何origin的调用返回的值。std::ftell在与同一文件%28关联的流上,该文件仅用于originSEEK_SET29%。

如果stream是面向广泛的,文本流和二进制流的限制适用于std::ftell被允许与寻求[医]设置和零偏移是允许从搜寻。[医]集合和寻找[医]但不是寻求[医]结束%29

除了更改文件位置指示器外,fseek消除…的影响std::ungetc并清除文件结束状态,如果适用的话。

如果发生读或写错误,则流%28的错误指示符std::ferror设置%29,文件位置不受影响。

参数

stream

-

file stream to modify

offset

-

number of characters to shift the position relative to origin

origin

-

position to which offset is added. It can have one of the following values: SEEK_SET, SEEK_CUR, SEEK_END

返回值

?0?如果成功,则非零值。

注记

在寻找宽流中的非结束位置之后,对任何输出函数的下一次调用可能会使文件的其余部分未定义,例如,通过输出不同长度的多字节序列。

POSIX允许在现有的文件末尾之外进行查找。如果在此搜索之后执行输出,则从间隙读取的任何数据都将返回零字节。在文件系统支持的地方,这会创建一个稀疏文件...

POSIX还要求ffind首先执行fflush如果有任何未写入的数据%28,但是否恢复了Shift状态,则为实现定义的%29。标准的C++文件流保证刷新和不移位:std::basic_filebuf::seekoff...

二次

代码语言:javascript
复制
#include <cstdio>
#include <cstdint>
#include <vector>
#include <fstream>
#include <cassert>
 
int main()
{
    std::ofstream("dummy.nfo") << "sample data\n";
 
 
    std::FILE* fp = std::fopen("dummy.nfo", "rb");
    assert(fp);
 
    std::fseek(fp, 0, SEEK_END); // seek to end
    std::size_t filesize = std::ftell(fp);
 
    std::fseek(fp, 0, SEEK_SET); // seek to start
    std::vector<uint8_t> buffer(filesize);
    std::fread(buffer.data(), sizeof(uint8_t), buffer.size(), fp);
 
    std::fclose(fp);
    std::printf("i've read %zi bytes\n", filesize);
}

二次

产出:

二次

代码语言:javascript
复制
i've read 12 bytes

二次

另见

fsetpos

moves the file position indicator to a specific location in a file (function)

fgetpos

gets the file position indicator (function)

ftell

returns the current file position indicator (function)

rewind

moves the file position indicator to the beginning in a file (function)

c文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com