请问有什么好的办法用Visual Basic读取600M到1G的dat文件(一个dat文件),我现在急需要,急需呀...

luckychr 2002-02-21 02:35:11
请问有什么好办法用Visual Basic读取600M到1G的dat文件(一个dat文件),我现在急需要,如有,给我源码好吗?
我的下面方法如下,但不能成功读取
Private Function readDatFile(ByVal strFileName As String) As Integer
Dim strLine
Dim strFile As String
Dim intRowCount As Integer
Dim intLoop As Integer

On Error GoTo Error1

intRowCount = 0
strFile = Trim(strFileName)
Filenum = FreeFile
Open strFile For Input As Filenum
Do While Not EOF(Filenum)
intRowCount = intRowCount + 1
Line Input #Filenum, strLine

Text1.Text = Text1.Text + strLine '+ Chr(13) + Chr(10)
' If intRowCount > 190 Then
' Exit Do
' End If
Loop
Close Filenum
readDatFile = 0
Exit Function

Error1:
readDatFile = 1
End Function

按我上述办法只能读取第一行,文件就自到跳至文件尾。以至无法读取后面的数据。请问各位大侠这是为什么呀?
...全文
128 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Chice_wxg 2002-02-21
  • 打赏
  • 举报
回复
是呀是呀!千万别指望全读到内存里。

因为这样系统还要再建立虚拟内存交换文件,到头来效果还不如不读呢,呵呵
Ninputer 2002-02-21
  • 打赏
  • 举报
回复
全部读入内存是很夸张的!不如采用流技术,只从所需的地方开始读入
raeck 2002-02-21
  • 打赏
  • 举报
回复
你可以分多次读取文件~ 
以binary方式能够读取指定大小的。
Chice_wxg(我怕谁?我是谁!我是流氓我最贼。) 的方法是可以的,源码我没有看,你试试啦~~
iwzw 2002-02-21
  • 打赏
  • 举报
回复
up up
AP 2002-02-21
  • 打赏
  • 举报
回复
只要你不把读到的内容都放到内存中文件的大小是无所谓的,最好用块方式读文件
Chice_wxg 2002-02-21
  • 打赏
  • 举报
回复

用C就Easy了,用 MapView,速度很快!

不过VB没操作过。

推荐用
open ... for binary as #1
二进制操作。
使用 Get/Put读写,Seek 定位。

实在不行可以参考API:
CreateFile() 或 OpenFile
ReadFile/WriteFile
用法都很简单。



这里有个例子:
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()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
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
cqq_chen 2002-02-21
  • 打赏
  • 举报
回复
没有操作过这么大的文件,帮不上了你,关注吧!
shawls 2002-02-21
  • 打赏
  • 举报
回复


关键是数据的存取,600m-1g不会要那么多巴!


全部反倒内存里面,有这么大的内存吗?
y1g1y1 2002-02-21
  • 打赏
  • 举报
回复
使用二制模式读取:
Open strFile For Binary As Filenum
网络咖啡 2002-02-21
  • 打赏
  • 举报
回复
我想帮你,可是我不知道,呵呵
学习

741

社区成员

发帖
与我相关
我的任务
社区描述
VB 版八卦、闲侃,联络感情地盘,禁广告帖、作业帖
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧