VBA中的seek与matlab中的fseek的说明

VBA: Seek 语句

语法:Seek [#]filenumber, position
其中position 为介于 1~ 2,147,483,647(相当于 2^31 – 1)之间的数字,指出下一个读写操作将要发生的位置。

功能:在 Open 语句打开的文件中,设置下一个读/写操作的位置。

说明:可以用Seek语句指定Get语句的读取位置,但在 Get 及 Put 语句中指定的记录号将覆盖由 Seek 语句指定的文件位置。

示例:
Dim MaxSize, NextChar, MyChar
Open "TESTFILE" For Input As #1     
MaxSize = LOF(1)       ' 取得文件的总字符数。
' 用循环读入所有记录,但是从最后的记录开始往前读。
For NextChar = MaxSize To 1 Step -1   
    Seek #1, NextChar      ' 设置读写位置。
    MyChar = Input(1, #1)      ' 读入一字符。
Next NextChar
Close #1

 

  

VBA、Seek 函数

语法:Seek(filenumber)

功能:返回一个 Long,在 Open 语句打开的文件中指定当前的读/写位置。

说明:在使用Get语句读取文件时,必须用LOF函数来判断是否到达文件末尾,而不是用EOF函数。可以使用Seek函数判断当前位置,然后与LOF的值比较。

示例:
Do While Seek(1) < LOF(1)
   '继续读取
   ......
Loop

 

 

Matlab 的fseek函数

help fseek
 FSEEK Set file position indicator.
    STATUS = FSEEK(FID, OFFSET, ORIGIN) repositions the file position
    indicator in the file associated with the given FID.  FSEEK sets the
    position indicator to the byte with the specified OFFSET relative to
    ORIGIN.
 
    FID is an integer file identifier obtained from FOPEN.
 
    OFFSET values are interpreted as follows:
        >= 0    Move position indicator OFFSET bytes after ORIGIN.
        < 0    Move position indicator OFFSET bytes before ORIGIN.
 
    ORIGIN values are interpreted as follows:
        'bof' or -1   Beginning of file
        'cof' or  0   Current position in file
        'eof' or  1   End of file
 
    STATUS is 0 on success and -1 on failure.  If an error occurs, use
    FERROR to get more information.
 
    Example:
 
        fseek(fid,0,-1)
 
    "rewinds" the file.
 

 

doc fseek:

fseek


Set file position indicator

Syntax

status = fseek(fid, offset, origin)

 

Description


status = fseek(fid, offset, origin) repositions the file position indicator in the file with the given fid to the byte with the specified offset relative to origin.

For a file having n bytes, the bytes are numbered from 0 to n-1. The position immediately following the last byte is the end-of-file, or eof, position. You would seek to the eof position if you wanted to add data to the end of a file.

This figure represents a file having 12 bytes, numbered 0 through 11. The first command shown seeks to the ninth byte of data in the file. The second command seeks just past the end of the file data, to the eof position.

 

fseek does not seek beyond the end of file eof position. If you attempt to seek beyond eof, MATLAB returns an error status.

Arguments


fid An integer file identifier obtained from fopen
offsetA value that is interpreted as follows,
offset > 0Move position indicator offset bytes toward the end of the file.offset = 0Do not change position.offset < 0Move position indicator offset bytes toward the beginning of the file.originA string whose legal values are
'bof'-1: Beginning of file'cof'0: Current position in file'eof'1: End of filestatusA returned value that is 0 if the fseek operation is successful and -1 if it fails. If an error occurs, use the function ferror to get more information.

Examples


This example opens the file test1.dat, seeks to the 20th byte, reads fifty 32-bit unsigned integers into variable A, and closes the file. It then opens a second file, test2.dat, seeks to the end-of-file position, appends the data in A to the end of this file, and closes the file.
fid = fopen('test1.dat', 'r');
fseek(fid, 19, 'bof');
A = fread(fid, 50, 'uint32');
fclose(fid);

fid = fopen('test2.dat', 'r+');
fseek(fid, 0, 'eof');
fwrite(fid, A, 'uint32');
fclose(fid)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值