如何使用input和lof函数完整读取文件(包含格式)

vinegar111 2006-10-17 10:22:23
lof(文件号)函数返回的是文件的字节数
而input函数用的是字符数
如果文本文件里包含中文和英文,那么用input(lof(文件号),文件号)就会溢出

有什么解决办法没有
...全文
295 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dunzip 2006-10-17
  • 打赏
  • 举报
回复
Private Function TxtRead(ByVal sFile As String) As String
On Error Resume Next
Dim TextLine As String
Open sFile For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
TxtRead = TxtRead & TextLine
Loop
Close #1
End Function

-------------------------------------------
例子
msgbox txtread("C:\Text.txt")
fxy_2002 2006-10-17
  • 打赏
  • 举报
回复
Dim sFile As String
Open "C:\filename.txt" For Input As #1
sFile = StrConv(InputB$(LOF(1), #1), vbUnicode)
Close #1


以上方法最好只处理640k以下的文件,超出的话可能会出错。

终极方式是 binary 读取,任何类型任意长度都没问题。
Dunzip 2006-10-17
  • 打赏
  • 举报
回复
当今最牛的代码

Private function TxtRead(byval sFile as string) as string
on error resume next
Open sFile For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
TxtRead=TxtRead & TextLine
Loop
Close #1
End function

-------------------------------------------
例子
msgbox txtread("C:\Text.txt")
happy_sea 2006-10-17
  • 打赏
  • 举报
回复
如果非得一次读入,可以用RichTextBox
happy_sea 2006-10-17
  • 打赏
  • 举报
回复
好像不大好办,因为在打开这个文件之前并不能事先知道其中究竟有多少中文多少英文,也就无法把字节数换算成字符数。难道非得一次读入吗?
Dunzip 2006-10-17
  • 打赏
  • 举报
回复
就用其他方法啊
vinegar111 2006-10-17
  • 打赏
  • 举报
回复
因为我想把原本文件里边的格式也读出来,txt文件格式很简单,就是回车换行什么的
用lineinput语句会删掉文本里原有的回车换行符,导致格式出问题。

7,765

社区成员

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

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