在线求救:如何用VB打开TXT文件,并读取里面的内容

programbug 2004-08-19 05:45:57
我有一TXT文件,内容如下:
100
120
112
134
。。。
文本内容用行分开,请问如何写程序,如何读取里面的内容,并求出有多少行,各行加起来的平均值是多少,如上面就应是:(100+120+112+134)/4
...全文
1511 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
frankwong 2004-08-19
  • 打赏
  • 举报
回复
'然后
行数=iRe.fields(0)
平均值=iRe.fields(0)
northwolves 2004-08-19
  • 打赏
  • 举报
回复
Private Sub Command1_Click()

Open "c:\123.txt" For Output As #1
For i = 1 To 100
Print #1, , i
Next
Close #1

GETIT "c:\123.txt"
End Sub
Sub GETIT(ByVal FILEPATH As String, Optional ByRef LINECOUNTS As Long, Optional ByRef AVG As Double)
Dim TEMP() As Byte, RESULT() As String, i As Long
AVG = 0
Open FILEPATH For Binary As #1
ReDim TEMP(1 To LOF(1))
Get #1, , TEMP
Close
RESULT = Split(StrConv(TEMP, vbUnicode), vbCrLf)
linecount = UBound(RESULT) + 1
For i = linecount To 1 Step -1
If Trim(RESULT(i - 1)) = "" Then linecount = linecount - 1
AVG = AVG + Val(RESULT(i - 1))
Next
AVG = FormatNumber(AVG / linecount, 2, vbTrue)
MsgBox "行数: " & linecount & vbCrLf & "均值: " & AVG
Close #1
End Sub


frankwong 2004-08-19
  • 打赏
  • 举报
回复
'要计算的为什么不用ado打开文本文件呢?那才叫爽.尤其是文件很大的时候
'先引用ado2.0
Dim iConc As String
iConc = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=""Text;HDR=NO" & ";DATABASE=c:\""" 'c:\文件目录
Set iRe = New ADODB.Recordset
With iRe
.CursorLocation = adUseClient
'abc#txt是要读取的文件名:abc.txt
.Open "select count(f1) from [1#txt]", iConc, adOpenKeyset, adLockOptimistic 'f1为第一列,[1#txt]为1.txt文件'本语句计算行数
.Open "select avg(f1) from [1#txt]", iConc, adOpenKeyset, adLockOptimistic 'f1为第一列,[1#txt]为1.txt文件,本语句计算平均值
End With
dongge2000 2004-08-19
  • 打赏
  • 举报
回复
以上是两个方法,第一个比FSO的快很多!
dongge2000 2004-08-19
  • 打赏
  • 举报
回复
'引用——Microsoft Scripting Runtime

Option Explicit

Function FsoRead(ByVal PFile As String) As String
On Error Resume Next
Dim u As Object
Dim X As TextStream
Set u = CreateObject("scripting.filesystemobject")
Set X = u.OpenTextFile(PFile, ForReading, True)
FsoRead = X.ReadAll
Exit Function
End Function

Function FsoWrite(ByVal PFile As String, ByVal OutFile As String) As Boolean
On Error GoTo Er
Dim Ob As Object
Dim X As TextStream
Set Ob = CreateObject("scripting.filesystemobject")
Set X = Ob.OpenTextFile(PFile, ForWriting, True)
X.Write OutFile
Exit Function
Er:
Dim y As FileSystemObject
Set y = CreateObject("scripting.filesystemobject")
Set Ob = y.CreateTextFile(PFile, True)
X.Write OutFile
End Function
dongge2000 2004-08-19
  • 打赏
  • 举报
回复
Option Explicit
'vbCrLf

Public Function WirteFile(ByVal TXTFile As String, ByVal inText As String) As Long
Dim fn As Integer
fn = FreeFile
Open TXTFile For Binary As #fn
Put #fn, , inText
Close #fn
End Function

Function ReadFile(TXTFile As String) As String
Dim fn As Integer
fn = FreeFile
Open TXTFile For Binary Access Read As #fn
ReadFile = Space(FileLen(TXTFile))
Get #fn, , ReadFile
Close #fn
End Function
programbug 2004-08-19
  • 打赏
  • 举报
回复
ajianchen2002(爱已逝) ,各数据是通过回车分开的,不是通过空格分开的,应该怎样办呀
ajianchen2002 2004-08-19
  • 打赏
  • 举报
回复
'重新打开文件读取数据
Open App.Path & "\data\grinderrectangleplaneap.txt" For Input As #1
'对数组进行赋值
i = 0
Do While Not EOF(1)
Line Input #1, TextLine '读取一行
ArrayLine = Split(TextLine, " ")
For j = 0 To UBound(ArrayLine) - 1
Singleap2(i, j) = CSng(ArrayLine(j))
Next j
i = i + 1
Loop
Close #1
haiz_2001 2004-08-19
  • 打赏
  • 举报
回复
F了
starsoulxp 2004-08-19
  • 打赏
  • 举报
回复
楼上正解
zcm123 2004-08-19
  • 打赏
  • 举报
回复
to: starsoulxp(星魂.NET)

-_-速度好快啊~
zcm123 2004-08-19
  • 打赏
  • 举报
回复
dim i,str,vstr
open "xxx.txt" for input as #1
Do While Not EOF(1)
Line Input #1,str ' 读入一行数据
i=i+1
vstr=val(str)+vstr
Loop
text1=vstr/i

Close #1 ' 关闭文件
starsoulxp 2004-08-19
  • 打赏
  • 举报
回复
读文件

Open "c:\1.txt" For Input As #f
Do While Not EOF(f)
Line Input #f, Content
tempInfo = tempInfo + Content + Chr(10) + Chr(13)
Loop
text1.text=tempInfo
Close f

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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