matlab中 seek,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)
總結
以上是生活随笔為你收集整理的matlab中 seek,VBA中的seek与matlab中的fseek的说明 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现手机扫码登录客户端
- 下一篇: 搭建全民K歌TV运行环境