如题,我用
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.ReadLine
s = a.ReadLine
MsgBox s
a.Close
但是报文件模式错误, 就是 a.ReadLine 这里出错。
...全文
484打赏收藏
如何读出文件其中的 一行?
如题,我用 Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile("c:\testfile.txt", True) a.ReadLine s = a.ReadLine MsgBox s a.Close 但是报文件模式错误, 就是 a.ReadLine 这里出错。
'引用microsoft script runtime
Private Function ReadFileLine(cfile As String, cLine As Integer) As String
Dim I As Integer
Dim fsoTest As New FileSystemObject, file1 As File, ts As TextStream
Set file1 = fsoTest.GetFile(cfile)
Set ts = file1.OpenAsTextStream(ForReading)
I = 1
Do While Not ts.AtEndOfStream
If I <> cLine Then
ts.ReadLine
Else
ReadFileLine = ts.ReadLine
End If
I = I + 1
Loop
MsgBox "总行数" & I
Set ts = Nothing
End Function
Private Sub Form_Load()
MsgBox "读取行的内容为:" & (ReadFileLine("f:\test.txt", 3))
Sub CreateAfile
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("This is a test.")
a.Close
End Sub
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending,TristateFalse)
f.Write "Hello world!"
f.Close
End Sub
直接用I/O函数实现就可以了
Private Sub Form_Load()
Dim FileNumber As Integer
Dim strFileName As String
Dim sTmp As String
Dim TextLine As String
FileNumber = FreeFile
Open strFileName For Input As #FileNumber
Line Input #FileNumber, TextLine
Close FileNumber
End Sub