Hướng dẫn dùng matlab fseek trong PHP

  • Định Nghĩa.
  • Cú pháp.
    • Cú pháp:
    • Trong đó.
    • Giá trị trả về.
  • Ví dụ.
    • Ví dụ # 1 Một ví dụ đơn giản về fseek()
      • code:
  • Ghi chú.
  • Hàm liên quan.
  • Thông tin thêm.

Định Nghĩa.

Hàm fseek()sẽ đặt vị trí cho con trỏ tệp tin của file được truyền vào. Vị trí mới sẽ được tính bằng số byte của các kí tự tính từ đầu file.

Cú pháp.

Cú pháp:

fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] ) : int

Trong đó.

  • $handle là file đã được mở trước đó bằng hàm fopen().
  • $offset là vị trí con trỏ tệp tin sẽ đến.
  • $option là tham số mang 1 trong các giá trị sau:
    • SEEK_SET: vị trí $offset tính bằng bytes.
    • SEEK_CUR: vị trí sau cùng sẽ bằng vị trí hiện tại + $offset.
    • SEEK_END: vị trí sau cùng sẽ bằng vị trí cuối của file + $offset.

Giá trị trả về.

  • Hàm fseek() sẽ trả về 0 nếu thành công, nếu thất bại hàm sẽ trả về False.

Ví dụ.

Ví dụ # 1 Một ví dụ đơn giản về fseek()

code:

Ghi chú.

  • Nếu bạn đã mở tệp ở chế độ append  (a hoặc +), mọi dữ liệu bạn ghi vào tệp sẽ luôn được nối, bất kể vị trí tệp và kết quả của việc gọi fseek () sẽ không được xác định.
  • Không phải tất cả các luồng hỗ trợ tìm kiếm. Đối với những người không hỗ trợ tìm kiếm, việc tìm kiếm chuyển tiếp từ vị trí hiện tại được thực hiện bằng cách đọc và loại bỏ dữ liệu; các hình thức tìm kiếm khác sẽ thất bại.

Hàm liên quan.

  • ftell() – Trả về vị trí hiện tại của con trỏ đọc / ghi tệp
  • rewind() – Tua lại vị trí của một con trỏ tệp

Thông tin thêm.

    • nếu bạn đang sử dụng fseek () để ghi dữ liệu
      $fp=fopen($filename,"r+");
      KHÔNG mở tệp ở chế độ “a” (để nối thêm), vì nó đặt con trỏ tệp ở cuối tệp và không cho phép bạn fseek vị trí trước đó trong tệp (nó không dành cho tôi!). Cũng thế, không mở tệp ở chế độ “w” – mặc dù điều này đặt bạn ở phần đầu của tệp – vì nó xóa sạch tất cả dữ liệu trong tập tin.
    • Các tài liệu chính thức chỉ ra rằng không phải tất cả các luồng đều có thể tìm kiếm được. Bạn có thể cố gắng tìm kiếm bằng mọi cách và xử lý thất bại:
       Hoặc, bạn có thể sử dụng hàm stream_get_meta_data: 

Các bạn có thể xem chi tiết hơn trên php.net.

Hi vọng với bài viết này, bạn đã hiểu rõ ứng dụng của hàm fseek() trong PHP. Nếu bạn thấy bài viết hay và có ý nghĩa hãy like và chia sẻ bài viết này để mọi người cùng nhau học tập nhé. Cảm ơn các bạn đã ghé thăm codetutam.com

fseek

Move to specified position in file

Syntax

Description

status = fseek(___) returns 0 when the operation is successful. Otherwise, fseek returns -1. Use any of the previous input argument combinations.

Examples

collapse all

Move to New Position in File

Open the following badpoem.txt file and perform read operations (which advance the position pointer) and then use seek to move to a new position in the file.

Hướng dẫn dùng matlab fseek trong PHP

Use fopen to open the file. Then, use ftell to query the current position.

fid = fopen('badpoem.txt');
ftell(fid)

Read the first three lines and query the position in the file after each read. Use fgetl to read and fseek to examine the current position after the read operation.

tline1 = fgetl(fid)  % read the first line 

tline1 = 
'Oranges and lemons,'

Read the second line and examine the current position.

tline2 = fgetl(fid)  % read the second line 

tline2 = 
'Pineapples and tea.'

Read the third line and examine the current position.

tline3 = fgetl(fid)  % read the third line 

tline3 = 
'Orangutans and monkeys,'

To read line 2, set the position in the file to point to the beginning of line 2. Use fseek to set the position, and then perform a read operation.

fseek(fid,20,'bof');
fgetl(fid)

ans = 
'Pineapples and tea.'

Close the file.

Input Arguments

collapse all

fileID — File identifier integer

File identifier of an open file, specified as an integer. Before using fseek, you must use fopen to open the file and obtain its fileID.

Data Types: double

offset — Number of bytes integer

Number of bytes to move from origin, specified as an integer. The value of offset can be positive, negative, or zero.

Data Types: double

origin — Starting location integer | character vector | string

Starting location in the file, specified as a character vector, string scalar, or a scalar number.

'bof' or -1

Beginning of file

'cof' or 0

Current position in file

'eof' or 1

End of file

Data Types: double | char | string

Tips

  • If a file has n bytes of data, then those n bytes are in positions 0 through n-1.

Alternatives

To move to the beginning of a file, call

This call is identical to

Extended Capabilities

C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

  • When the MATLAB® behavior differs from the C compiler behavior, the generated code matches the C compiler behavior. Examples include:

    • Seeking past the end of a file.

    • Seeking away from the end of a file opened with append access.

  • The offset is passed to the C run-time environment as a signed long data type. Therefore, the offset value must fit in the long data type on the target hardware.

Version History

Introduced before R2006a