A. VB如何读取大的文本文件
记得好象一般上限是2G,再要大的话就难解决了,网上也贴过好多帖子,没几个想得出好办法..不知道楼主文件多大啊?你后面删掉了读取的语句,当然可以显示出“打开成功”你要在“打开成功”后加个DoEvents以前的代码也可以显示出来.我一直是用二进制打开方式操作那些给人看的文本文件,一次性读入所有的内容,从5M的文件开始有点吃力了.写点代码吧,你拿去试下,看对你有帮助不.按你的意思,边读文件,边将文件内容显示出来你需要画一RichTextBox,两个按钮RichTextBox的名称为RichText1, ScrollBars=2两个Command名称Command1 Caption="Read File" 开始读文件Command2 Caption="Stop" 停止读文件读取文件为"C:\1.txt"窗体代码:Dim iStop As IntegerPrivate Sub Command1_Click()iStop = 0Dim Data1 As StringDim Fn As LongDim lFileLong As DoubleDim lReadL As DoubleDim lReadLs As DoubleDim iSpeed As IntegeriSpeed = 10lReadL = 1024lReadL = lReadL * iSpeedlReadLs = 0RichText1.Text = ""Fn = FreeFileOpen "c:\1.txt" For Binary As #FnlFileLong = LOF(Fn)Do While lReadLs < lFileLong And iStop = 0 If lFileLong – lReadLs <= lReadL Then lReadL = lFileLong – lReadLs Seek #Fn, lReadLs + 1 DoEvents Data1 = Input$(lReadL, #Fn) lReadLs = lReadLs + lReadL RichText1.Text = RichText1.Text & Data1 DoEventsLoopClose #FnMsgBox "读入文件完毕!"End SubPrivate Sub Command2_Click()iStop = 1End Sub补充说明:iSpeed值可以随你设,最小为1,最大65535,意思就是每次读入数据的多少,iSpeed=1的时候每次读入1024 Bytes数据,并将其显示在RichTextBox,iSpeed越小,读取间隔越短,但是读入效率低,你可以比较随意的拖动滚动条到你想浏览的地方;iSpeed越大,读取间隔越长,把文件完整读入所需的时间相对短,但是过大的话你就不容易拖动滚动条.你可以在读取文件的同时浏览已经读入的文本数据.我没测试最大能读多大的文件..也不知道你需要读多大的文件.(理论上我猜测越读到后面越慢.因为RichTextBox.Text的属性操作越到后面需要的内存越大)补充:以下代码改下,应该可以满足要求.Dim iStop As IntegerPrivate Sub Command1_Click()Dim sfFileName As String '文件名Dim sReadData As StringDim Fn As LongDim lFileLong As DoubleDim lReadL As DoubleDim lReadLs As DoubleDim iSpeed As IntegersfFileName = App.Path & "\ywtest.tfm"iSpeed = 5lReadL = 1024lReadL = lReadL * iSpeedlReadLs = 0sReadData = ""Fn = FreeFileOpen sfFileName For Binary As #FnlFileLong = LOF(Fn)Do While lReadLs < lFileLong And iStop = 0 If lFileLong – lReadLs <= lReadL Then lReadL = lFileLong – lReadLs Seek #Fn, lReadLs + 1 DoEvents sReadData = sReadData & Input$(lReadL, #Fn) '将上次未完成处理的字符和本次读取的字符合并在一起 lReadLs = lReadLs + lReadL Call ListAddItem(sReadData) '调用函数处理所得的字符串 DoEventsLoopClose #FnCall ListAddItem(sReadData, True) '处理未完成处理的最后剩余字符MsgBox "读入文件完毕!"End SubPrivate Sub Command2_Click()iStop = 1End Sub'将读出的行添加到列表'变量说明:'sDealStr (传地址) 读取出来所处理的字符串'isWithOutEnter 如果字符串结尾没有回车,是否将剩余字符判断为一行(用于文件最后一行有时没有回车换行的情况)Private Sub ListAddItem(ByRef sDealStr As String, Optional isWithOutEnter As Boolean = False)Dim ln As Double '查找回车换行字符储存的地址Dim lnLast As Double '上一次的地址Dim longDealStr As Double '字符串长度Dim sFindStr As String '一行字符的保存变量longDealStr = Len(sDealStr)ln = 0lnLast = -1Do ln = InStr(ln + 1, sDealStr, vbCrLf) '在sDealStr中查找回车换行符 If ln = 0 Then If isWithOutEnter = False Then '不提取剩余字符,直接退出 Exit Do Else '准备将剩余字符提取 ln = longDealStr + 1 End If End If sFindStr = Mid(sDealStr, lnLast + 2, ln – lnLast – 2) '提取一行字符 List1.AddItem sFindStr '添加到列表(此处你可以调用你的代码处理sFindStr,即你的linestr lnLast = ln DoEventsLoop While ln < longDealStr '如果查找到地址小于总长度则继续查找下一个If longDealStr – lnLast – 1 > 0 Then If lnLast > 0 Then '如果最后一次的地址存在则将该地址之前的字符全部去掉 sDealStr = Right(sDealStr, longDealStr – lnLast) '此时sDealStr只剩余没处理完的字符 End IfElse '如果地址结束则直接清空 sDealStr = ""End IfEnd Sub在Form里画一个ListBox和两个Command
B. vb里的文件号最大取值是多少
511,书上说的
C. VB编程中的文件号是怎么回事
文件号是你自己定义的一个号码,无所谓常用与否。实际上最常用的无非是1、2、3、4等等数字。建议使用1~255之间的号码,也可以使用256~511之间的。可以使用FreeFile函数返回一个空闲文件号。举例:open "C:\WINDOOWS\System32\cmd.exe" for input as 1