❶ VB如何读取txt数据
Private
Sub
Command1_Click()
'基本读取方法---只能读取非中文
'
Dim
a
As
String,
b
As
String,
c
As
String,
d
As
String,
e
As
String,
f
As
String'定义几个变量
Dim
FileNo
As
Integer
FileNo
=
FreeFile()
'获取一个未使用的文件号
Dim
str
As
String
'用来记录最终的值
Open
"你要读取文件的完整路径"
For
Input
As
#FileNo
While
Not
EOF(FileNo)
Dim
str_Read
As
String
Input
#FileNo,
str_Read
'读取一个字符到变量str_Read---不包含换行回车符,也不包括逗号,分号及Tab,当读到分隔符号(前面列举的4种)时就赋值给后面的变量,如果有多个变量就读取相对应多的分隔数据
'如果为你的007.txt文件且为你列举的内容那么上面一句就改成下面的语句
'
Input
#FileNo,
a,
b,
c,
d,
e,
f
'
str
=
str
&
str_Read
Wend
'Me.Text1.Text
=
str
Close
#FileNo
End
Sub
❷ vb如何读取多行txt文本
1、对于文件的操作vb提供了open语句、print #语句、input #语句等语句来处理文件。
2、读取多行文本,可以使用Line input #语句,一行行读取,再连接在一起,这种方法对大一点txt文件效率不高。
3、下面提供一个高效率、一次性读入所有文本的vb6示例:
PrivateSubCommand1_Click()
Dimr
Open"d: eadme.txt"ForBinaryAs#1
r=StrConv(InputB(LOF(1),1),vbUnicode)
Close#1
Debug.Printr
EndSub
❸ VB6中如何逐行读取文本文件txt中的文本,每次按一次窗体取出下一条信息
Dim MyStr() As String
Private Sub Command1_Click()
Dim t1 As Long, t2 As Long, t3 As Long
Randomize
t1 = Int(UBound(MyStr) * Rnd + 1)
Text1.Text = MyStr(t1)
If UBound(MyStr) < 2 Then Exit Sub
Do
t2 = Int(UBound(MyStr) * Rnd + 1)
Loop While t2 = t1
Text2.Text = MyStr(t2)
If UBound(MyStr) < 3 Then Exit Sub
Do
t3 = Int(UBound(MyStr) * Rnd + 1)
Loop While t3 = t2 Or t3 = t1
Text3.Text = MyStr(t3)
End Sub
Private Sub Form_Load()
Dim fn As Integer, Str As String
ReDim MyStr(0)
fn = FreeFile
Open "C:\TXT.txt" For Input As #fn 'TXT 是文件名
Do Until EOF(fn)
Line Input #fn, Str
If Str <> "" Then
ReDim Preserve MyStr(UBound(MyStr) + 1)
MyStr(UBound(MyStr)) = Str
End If
Loop
Close #fn
End Sub
❹ VB6.0中如何实现逐行读入文本文件
VB6.0可以用一次读取文本文件全部文本内容,然后使用Split函数来实现一行一行提取文本框行内容。
Split函数,返回一个下标从零开始的一维数组,它包含指定数目的子字符串。
PrivateSubCommand1_Click()
DimstrWjAsString
Dimstrj()AsString
DimaryContent()AsByte
DimiAsLong
DimjAsLong
CommonDialog1.CancelError=True'设置“CancelError”为True
OnErrorGoToErrHandler
CommonDialog1.Flags=cdlOFNHideReadOnly'设置标志
'设置过滤器
CommonDialog1.Filter="AllFiles(*.*)|*.*|TextFiles"&"(*.txt)|*.txt|BatchFiles(*.bat)|*.bat"
CommonDialog1.FilterIndex=2'指定缺省的过滤器
CommonDialog1.ShowOpen'显示“打开”对话框
'显示选定文件的名字
'MsgBoxCommonDialog1.FileName
OpenCommonDialog1.FileNameForBinaryAs#1
ReDimaryContent(LOF(1)-1)
Get#1,,aryContent
Close#1
strWj=StrConv(aryContent,vbUnicode)
RichTextBox1=strWj
Text1=strWj
strj=Split(strWj,vbCrLf)
i=UBound(strj)
Text2=i+1
j=InputBox("输入需要显第几句",j)
j=j-1
Label1.Caption=j+1&":"&strj(j)
ExitSub
ErrHandler:
'用户按了“取消”按钮
ExitSub
EndSub
❺ 请问VB中如何读取txt文件的数据
1、新建一个标准的VB EXE工程,只有一个Form,Form上有两个按钮:Command1和Command2。
❻ vb 怎样读取TXT文件中的数据
代码如下:Private
Sub
Command1_Click()Open
"F:\数据.txt"
For
Input
As
#1Input
#1,
a,
b,
c,
dPrint
"a="&a,"b="&
b,"c="&c,"d="&dClose
#1End
Sub-几种打开文件方法-------------------------1.open
<文件名>
for
input
as#<文件号>如果文件不存在则会出错,文件号将在以后操作中取代文件名,范围是[1,511]。2.open
<文件名>
for
output
as#<文件号>建立一个新文件并打开它,如果文件存在就删了再建立。3.open
<文件名>
for
append
as#<文件号>为了在文件尾部追加内容而打开,写入数据时是从尾部进行。-读取-------------------------------------1.input
#<文件号>,<变量表>读出一行数据并依次分配给各变量2.line
input
#<文件号>,<变量名>读出一行数据直到回车换行之前的全给此变量------------------------------------------
❼ VB2010 如何在循环指定文件夹后将里面的所有文件挨个逐行读取
Dim Files() As String
.........
Files = IO.Directory.GetFiles("E:\VB2010\", "*.txt")
你前面定义files()时是字串数组,而这里又赋值为文件对象给它,当然不正确