fseek的使用

一:概述

  在官方文档里,对于fseek的描述是

    Move to specified position in file,移到文件的某一个特殊位置

二:语法

  status = fseek(fileID, offset, origin)

  fileID的意思是fopen打开时产生的整数标识,大于0时,表示文件成功打开。

  在文件中,offset是,相对于起始origin的位置开始移动的整数

  origin有三种状态,bof,cof,eof,其分别表示文件的开始位置,当前位置,和末尾位置。

  如果操作成功,则状态返回0,否则返回为-1.

三:举例,方便加深理解

  要求:Copy 5 bytes from the file test1.dat, startingat the tenth byte, and append to the end of test2.dat

  解析:有两个文件,test1.dat,test2.dat

      在文件test1.dat的第10个位置,复制5个byte到test2.dat的末尾处。

  程序:

    % Create files test1.dat and test2.dat
    % Each character uses 8 bits (1 byte)

 

    %open test1 and type is w+,then write char

    fid1 = fopen('test1.dat', 'w+');
    fwrite(fid1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');

 

    %open test2 and type is w+,then write char

    fid2 = fopen('test2.dat', 'w+');
    fwrite(fid2, 'Second File');

 

    % Seek to the 10th byte ('J'), read 5
    fseek(fid1, 9, 'bof');
    A = fread(fid1, 5, 'uint8=>char');%%%%%%%%%%fread
    fclose(fid1);

 

    % Append to test2.dat
    fseek(fid2, 0, 'eof');
    fwrite(fid2, A);%%%%%%%%%%%%%%%%%%fwrite
    fclose(fid2);

四:综述

  通过fid找到操作的文件,然后找到文件要操作的具体位置

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值