我使用下面的代码想定位读文件的指针,想得到的效果是从我定位的位置开始读文件
代码如下:
SendStr = "REST 100"
SendStr1 = "RETR \test.txt"
FtpCommand(inHwnd, False, 1, SendStr, 0, FileHwnd)
FtpCommand(inHwnd, True, 1, SendStr1, 0, FileHwnd)
InternetReadFile(FileHwnd, Filestr, BuffSize, realSize)
现在程序读出来的效果是从第一个子节开始,我要他从第100个字节开始,请给简单代码?急
代码如下:
SendStr = "REST 100"
SendStr1 = "RETR \test.txt"
FtpCommand(inHwnd, False, 1, SendStr, 0, FileHwnd)
FtpCommand(inHwnd, True, 1, SendStr1, 0, FileHwnd)
InternetReadFile(FileHwnd, Filestr, BuffSize, realSize)
现在程序读出来的效果是从第一个子节开始,我要他从第100个字节开始,请给简单代码?急
说明:
设置文件中进行读写的当前位置。该函数与vb的seek语句类似。如果用vb的open命令打开了一个文件,那么不要再对这个文件使用llseek函数
Declare Function llseek Lib "kernel32" Alias "llseek" (ByVal hFile As Long, ByVal lOffset As Long, ByVal iOrigin As Long) As Long返回值:long,返回一个新位置,设置成从文件起始处算起的一个偏移量。HFILE_ERROR表示函数执行出错。会设置GetLastError
参数:
hFile ---------- Long,系统文件句柄 lOffset -------- Long,字节偏移量 iOrigin -------- Long,下述常数之一
FILE_BEGIN
lOffset将新位置指定成从文件起始处的一个偏移距离
FILE_CURRENT
lOffset将新位置指定成从当前位置开始的一个偏移距离
FILE_END
lOffset将新位置指定成从文件结尾开始的的一个偏移距离参考SetFilePointer函数,认识能对较大文件进行处理的一个近似函数.
说明:
在一个文件中设置当前的读写位置
声明:
Declare Function SetFilePointer Lib "kernel32" Alias "SetFilePointer" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
返回值:
Long,返回一个新位置,它采用从文件起始处开始算起的一个字节偏移量。HFILE_ERROR意味着出错。会设置GetLastError
参数:
hFile ---------- Long,系统文件句柄 lDistanceToMove - Long,字节偏移量 lpDistanceToMoveHigh - Long,指定一个长整数变量,其中包含了要使用的一个高双字偏移。可设为零(将声明变为ByVal),表示只使用lDistanceToMove
原文:A long variable containing a high double word offset to use. May be zero (change
declaration to ByVal) to use only lDistanceToMove. dwMoveMethod --- Long,下述常数之一
FILE_BEGIN
lOffset将新位置设为从文件起始处开始算的起的一个偏移
FILE_CURRENT
lOffset将新位置设为从当前位置开始计算的一个偏移
FILE_END
lOffset将新位置设为从文件尾开始计算的一个偏移这个函数与vb的seek语句类似。不要将函数用于通过vb的open命令打开的文件。利用这个函数,可以处理那些长度大于2^64字节的大型文件
例子:
Const MOVEFILE_REPLACE_EXISTING = &H1
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Private Declare Function SetVolumeLabel Lib "kernel32" Alias "SetVolumeLabelA" (ByVal lpRootPathName As String, ByVal lpVolumeName As String) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Sub Form_Load()
Dim sSave As String, hOrgFile As Long, hNewFile As Long, bBytes() As Byte
Dim sTemp As String, nSize As Long, Ret As Long
'Ask for a new volume label
sSave = InputBox("Please enter a new volume label for drive C:\" + vbCrLf + " (if you don't want to change it, leave the textbox blank)")
If sSave <> "" Then
SetVolumeLabel "C:\", sSave
End If
'Create a buffer
sTemp = String(260, 0)
'Get a temporary filename
GetTempFileName "C:\", "KPD", 0, sTemp
'Remove all the unnecessary chr$(0)'s
sTemp = Left$(sTemp, InStr(1, sTemp, Chr$(0)) - 1)
'Set the file attributes
SetFileAttributes sTemp, FILE_ATTRIBUTE_TEMPORARY
'Open the files
hNewFile = CreateFile(sTemp, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
hOrgFile = CreateFile("c:\config.sys", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
'Get the file size
nSize = GetFileSize(hOrgFile, 0)
'Set the file pointer
SetFilePointer hOrgFile, Int(nSize / 2), 0, FILE_BEGIN
'Create an array of bytes
ReDim bBytes(1 To nSize - Int(nSize / 2)) As Byte
'Read from the file
ReadFile hOrgFile, bBytes(1), Ubound(bBytes), Ret, ByVal 0&
'Check for errors
If Ret <> Ubound(bBytes) Then MsgBox "Error reading file ..."
'Write to the file
WriteFile hNewFile, bBytes(1), Ubound(bBytes), Ret, ByVal 0&
'Check for errors
If Ret <> Ubound(bBytes) Then MsgBox "Error writing file ..."
'Close the files
CloseHandle hOrgFile
CloseHandle hNewFile
'Move the file
MoveFileEx sTemp, "C:\KPDTEST.TST", MOVEFILE_REPLACE_EXISTING
'Delete the file
DeleteFile "C:\KPDTEST.TST"
Unload Me
End Sub
这些api函数都不行的
有个ftpsetfilepointer函数,不过使用的时候不能成功的设置读写指针。
rest命令在基础ftp命令中有文件读写偏移的作用,我有个vc的源代码(不全)就是使用rest实现文件指针偏移的。期待中