如何读出文件其中的 一行?

rednod 2003-08-20 12:10:37
如题,我用
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)

a.ReadLine
s = a.ReadLine
MsgBox s
a.Close

但是报文件模式错误, 就是 a.ReadLine 这里出错。
...全文
48 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihonggen0 2003-08-20
  • 打赏
  • 举报
回复
'引用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))



End Sub
道素 2003-08-20
  • 打赏
  • 举报
回复
而且CreateTextFile方法是创建文件,你应该用OpenTextFile

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

射天狼 2003-08-20
  • 打赏
  • 举报
回复
Dim ss() As String, i As Long

Open "c:\z.txt" For Input As #1
i = 0
While Not EOF(1)
ReDim Preserve ss(i + 1)
Input #1, ss(i) '存到数据组里,想取哪行都行~~
i = i + 1
Wend
Close #1
道素 2003-08-20
  • 打赏
  • 举报
回复
直接用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

7,759

社区成员

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

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