Welcome to my blog.|

Khru

园龄:2年11个月粉丝:22关注:1

📂C
2023-05-03 23:12阅读: 7评论: 0推荐: 0

C-随机访问

随机访问

int fseek(FILE *stream, long int offset, int whence)

  • stream -- 指向 FILE 对象的指针, 该 FILE 对象标识了流.
  • offset -- 这是相对 whence 的偏移量, 以字节为单位. 若为负则向前移.
  • whence -- 这是表示开始添加偏移 offset 的位置.
    • SEEK_SET -- 文件开头
    • SEEK_CUR -- 文件指针当前位置
    • SEEK_END -- 文件末尾
  • 如果成功, 返回0, 否则返回非零值.
FILE* file = fopen("test.txt", "r");
if(file != NULL)
{
fseek(file, 2, SEEK_SET);
putchar(getc(file));
fclose(file);
}

当前位置

long int ftell(FILE *stream)

该函数返回位置标识符的当前值. 如果发生错误, 则返回 -1L, 全局变量 errno 被设置为一个正值.

FILE* file = fopen("test.txt", "r");
if(file != NULL)
{
int len;
fseek(file, 0, SEEK_END);
len = ftell(file);
fclose(file);
printf("test.txt 的总大小 = %d 字节\n", len);
}

设定位置

FILE* file = fopen("test.txt", "r");
if(file != NULL)
{
fpos_t pos; // 使用fpos_t存储位置
fgetpos(file, &pos); // 获取位置
fseek(file, -2, SEEK_END); // 移动当前位置
fsetpos(file, &pos); // 设定位置
printf("%ld", ftell(file));
fclose(file);
}

本文作者:khrushchefox

本文链接:https://www.cnblogs.com/khrushchefox/p/17369895.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Khru  阅读(7)  评论(0编辑  收藏  举报
历史上的今天:
2022-05-03 SQL Server-包含not exists 谓词的嵌套相关子查询
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起