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
'要计算的为什么不用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
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
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
'重新打开文件读取数据
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
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