- File Manipulation
Win32 API Functions that create, read, and write to files:
CreateFile
、ReadFile
、WriteFile
、SetFilePointer
CreateFile
CreateFile
either creates a new file or opens an existing file
. If successful, it returns a handle to the open file; otherwise, it returns a special constant named INVALID_HANDLE_VALUE
.
- Prototype: 12345678CreateFile PROTO,pFilename:PTR BYTE, ; ptr to filenamedesiredAccess:DWORD, ; access modeshareMode:DWORD, ; share modelpSecurity:DWORD, ; ptr to security attribscreationDisposition:DWORD, ; file creation optionsflagsAndAttributes:DWORD, ; file attributeshtemplate:DWORD ; handle to template file
desiredAccess
- 0 (check for the existence of a file)
- GENERIC_READ
- GENERIC_WRITE
creationDisposition
- CREATE_NEW (the function fails if the file already exists)
- CREATE_ALWAYS
- OPEN_EXISTING
- OPEN_ALWAYS
- TRUNCATE_EXISTING
## CreateFile Examples
Opens an existing file for reading: 讀取並打開已經存在的檔案
Opens an existing file for writing: 寫入並打開已經存在的檔案
Creates a new file with normal attributes, erasing any existing file by the same name: 製造一個新檔案並刪除一樣名字的檔案
ReadFile
- ReadFile reads text from an input file
- Prototype:123456ReadFile PROTO,handle:DWORD, ; handle to filepBuffer:PTR BYTE, ; ptr to buffernBufsize:DWORD, ; num bytes to readpBytesRead:PTR DWORD, ; bytes actually readpOverlapped:PTR DWORD ; NULL (0) for syn mode
WriteFile
- WriteFile writes data to a file, using an output handle. The handle can be the screen buffer handle, or it can be one assigned to a text file.
- Prototype:123456WriteFile PROTO,fileHandle:DWORD, ; output handlepBuffer:PTR BYTE, ; pointer to buffernBufsize:DWORD, ; size of bufferpBytesWritten:PTR DWORD, ; num bytes writtenpOverlapped:PTR DWORD ; NULL (0) for syn mo
SetFilePointer
- SetFilePointer moves the position pointer of an open file. You can use it to append data to a file, and to perform random-access record processing:
Prototype:
12345SetFilePointer PROTO,handle:DWORD, ; file handlenDistanceLo:SDWORD, ; bytes to move pointerpDistanceHi:PTR SDWORD, ; ptr to bytes to movemoveMethod:DWORD ; starting pointExample:
123; Move to end of file:INVOKE SetFilePointer,fileHandle,0,0,FILE_END
- Example
|
|