你的程序只读了123txt中的第一行文字,可以肯定,这一行文字中少于2个逗号,也就是说a(2)是不存在的,所以下标越界。
如果你需要读取123txt中的全部文字,且123txt中有多行文字,你的程序是有问题的。
以下的代码演示了如何将一个文本文件一次性读到一个字符串变量中,方法是采用了一个和文本文件等长的字符串变量来接收文本
Dim
s
As
String
Open
"d:\123txt"
For
Binary
As
#1
s
=
Space(LOF(1))
Get
#1,
,
s
Close
#1
Text1Text
=
s
是一个简单的聊天程序的客户端,主要用到的控件有:
工具栏 :四个按钮 连接 登录 退出 保存
命令按钮
文本框:用于发送接收聊天信息
Winsock 控件一个
这只是一个客户端,想要完整的运行还需要服务器端程序的编写。
只要能弄懂控件的大体属性就可以了,这个要查MSDN,不需要弄太懂否则会花费很多额外的时间,看下主要的属性,举个例子,学习ADO很麻烦,但是只要弄懂ADO中的recordset和connection就可以了,对这两个又主要弄懂movefirst,movenext,open等几个就行,全弄懂得头疼死你
补充一下,rsfiled属性最重要忘了说了,弄好这几个属性基本上ADO的一般小 *** 作就OK了,VB的其它程序的学习也类似,80%的功能20%的控件完成,掌握好这20%就行了
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Dim pid As Long
Dim pname As String
Private Sub Command1_Click()
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
mydwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, myszExeFile, Chr(0))
mName = LCase(Left(myszExeFile, i - 1))
List1AddItem mName
' If mName = "1exe" Then
' pid = myth32ProcessID
' pname = mName
' MsgBox "找到word"
' If MsgBox("你想删除 " & mName & " 进程", vbYesNo + vbQuestion) <> vbYes Then
' Exit Sub
' End If
'
' Dim mProcID As Long
' mProcID = OpenProcess(1&, -1&, pid)
' TerminateProcess mProcID, 0&
'
' flag = True
' Exit Sub
' Else
' flag = False
' End If
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
End If
l1 = CloseHandle(l)
End If
' If flag = False Then
' MsgBox "没有找到word"
' Shell "c:\windows\notepadexe", vbNormalFocus
' End If
End Sub
1、新建一个标准的VB EXE工程,只有一个Form,Form上有两个按钮:Command1和Command2。
2、双击Command1添加如下代码
Private Sub Command1_Click()
Dim strFile As String
Dim intFile As Integer
Dim strData As String
strFile = "c:\学生成绩txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
DebugPrint strData
Close intFile
End Sub
3、按F8开始单步调试代码,点击Command1,进入单步调试功能,
4、多次按下F8或直接按下F5运行完成,就完成了读取文本文件内容并输出到立即窗口。
读文件
Dim s As String
Dim filename As String
filename = shuxiang & xingzuo & "txt"
Open filename For Input As #1
Do While Not EOF(1)
Line Input #1, s
Print s
Loop
Close #1
追加写文件
Dim i As Integer, n As Integer, a(6) As Integer
n = 6
Open "D:\1122txt" For Append As #1
Randomize
For i = 1 To n
a(i) = Int(Rnd 1000 + 1)
Print #1, a(i)
Next i
Close #1
覆盖写文件
Dim i As Integer, n As Integer, a(6) As Integer
n = 6
Open "D:\1122txt" For output As #1
Randomize
For i = 1 To n
a(i) = Int(Rnd 1000 + 1)
Print #1, a(i)
Next i
Close #1
'函数:一次性读文件至变量,非常快 Function GetFile(FileName As String) As String Dim i As Integer, s As String, BB() As Byte If Dir(FileName) = "" Then Exit Function i = FreeFile ReDim BB(FileLen(FileName) - 1) Open FileName For Binary As #i Get #i, , BB Close #i s = StrConv(BB, vbUnicode) GetFile = s End Function '调用举例: dim s as string s=GetFile("c:\1txt")
以上就是关于VB怎么读取文件中的全部信息全部的内容,包括:VB怎么读取文件中的全部信息、读vb程序、如何看懂VB程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)