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, MyCharOpen “TESTFILE” For Input As #1 MaxSize = LOF(1) ‘ 取得文件的总字符数。’ 用循环读入所有记录,但是从最后的记录开始往前读。For NextChar = MaxSize To 1 Step -1 Seek #1, NextChar ‘ 设置读写位置。 MyChar = Input(1, #1) ‘ 读入一字符。Next NextCharClose #1

VBA、Seek 函数

语法:Seek(filenumber)

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

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

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

Matlab 的fseek函数

help fseekFSEEK 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 fopenoffsetA 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)

只做第一个我,不做第二个谁。

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

相关文章:

你感兴趣的文章:

标签云: