I. first introduce the basic usage of file, seek, and truncate:
Seek (offset, where): Where = 0 moves from the starting position, 1 moves from the current position, and 2 moves from the ending position. When there is a line change, it will be truncated by a line break. Seek () has no return value, so the value is none.
Tell (): the current location of the file, that is, the location where tell obtains the file pointer, which is affected by seek, Readline, read, and readlines and is not affected by truncate.
Truncate (n): truncate from the beginning of the first line of the file. The truncated file contains n characters. If n is absent, all characters after N are deleted from the current position. The line feed in win represents the size of 2 characters.
Readline (n): reads several rows. N indicates the maximum number of bytes. The starting position of the read operation is tell () + 1. When N is null, the content of the current row is read-only by default.
Readlines read all rows
Read reads all rows
II. The following example shows the functions of the above functions.
FSO = open ("F: \ a.txt", 'W + ')' open the file in W + mode, not a mode, so the original content of the file is cleared.
The original content of the print FSO. Tell () 'file is cleared, so tell () = 0
FSO. Write ("ABCDE \ n") 'writes the file ABCDE \ n. Because the line feed \ n contains two characters, a total of seven characters are written.
Print FSO. Tell () 'at this time tell () = 7
FSO. Write ("fghwm") 'is written to the fghwm file. Therefore, the file is written 7 + 5 = 142 characters in total.
Print FSO. Tell () 'at this time tell () = 12
FSO. Seek (1, 0) 'moves one character from the starting position, that is, the first character in the first line of the file.
Print FSO. Tell () 'at this time tell () = 1
Print FSO. Readline () 'reads the current row, that is, the first row of the file, but starts reading from the second character (tell () + 1). The result is bcde.
'If you change to for to read the entire file or read to read the entire file, it is set to bcdefghwm.
Print FSO. Tell () 'Because Readline tell () = 7,
FSO. truncate (8) 'starts from the first line of the written file and is truncated to 8 characters, namely, ABCDE \ NF, that is, the file content is ABCDE \ NF
Print FSO. Tell () 'Tell () is still 7 and is affected by truncate (8), but the file content is ABCDE \ NF
Print FSO. Readline () 'reads the content of the current row from tell () + 1 = 8: F
FSO. Close ()