文本文件读取?

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总是空呢?
...全文
88 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
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

7,785

社区成员

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

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