文本文件读取?

workup 2003-12-26 09:15:51
Dim str As String
Open "f:\rav.txt" For Random As #1
Get #1, , str
Debug.Print str
Close #1

为什么STR总是空呢?
...全文
11 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
assa 2003-12-26
给你段代码吧

Private Function FileText(ByVal filename As String) As String
Dim handle As Integer
' 判断文件存在性
On Error GoTo err
If Len(Dir$(filename)) = 0 Then
err.Raise 53 '文件没有找到
End If
' 以binary模式打开文件
handle = FreeFile
Open filename$ For Binary As #handle
'读取内容,关闭文件
FileText = Space$(LOF(handle))
Get #handle, , FileText
Close #handle
Exit Function
err:
Exit Function
End Function
回复
workup 2003-12-26
现在可以打开TXT的文件了,但当打开如WORD文档,JPG图片时,为什么返回的还是空呢?
初学!
谢谢各位
回复
flyingscv 2003-12-26
str 是0长度,所以没东西
Dim str As String *10
就可以读出10个
不过for random 不好用,用什么都比它好:)
回复
rexyudl 2003-12-26
FSO
回复
northwolves 2003-12-26
Dim str As String
Open "f:\rav.txt" For Binary As #1
str = Space(FileLen("f:\rav.txt"), " ")
Get #1, , str
Debug.Print str
Close #1
回复
pcwak 2003-12-26
Private Sub Command1_Click()
Dim filenum As Integer
Dim fileContents As String
Dim fileInfo() As String
Dim i As Integer

filenum = FreeFile
Open "c:\1.txt" For Binary As #filenum
fileContents = Space(LOF(filenum))
Get #filenum, , fileContents
Close filenum
fileInfo = Split(fileContents, vbCrLf)

For i = 0 To UBound(fileInfo) - 1
Debug.Print fileInfo(i)
Next
Close #filenum
End Sub
回复
chao778899 2003-12-26
open filename for input as #1
input #1,str
close #1

msgbox str
回复
liuyanghe111 2003-12-26
open filename for input as FileNum

回复
昵称不腻 2003-12-26
文件格式不一样 random是记录型的
binary是2进制的,要指定读区位置
回复
wenhongL 2003-12-26
Dim str As String
Open "f:\rav.txt" For Binary As #1
while .eof
str = Space(FileLen("f:\rav.txt"), " ")
Get #1, , str
Debug.Print str
loop
Close #1
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告