1. 把一个字符串变量(>64KB)存入一个文件中.

dbbdggdbbdgg 2000-04-29 12:48:00
我想实现这样的功能
1. 把一个字符串变量(>64KB)存入一个文件中.
SaveFile(str as string, filename as string)
2. 把整个文件(>64K)读入一个字符串中.
str=LoadFile(filename)
以前都是用RichTextbox实现, 自己只用open, put, 之类的能实现吗? 怎么做?
...全文
136 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dbbdggdbbdgg 2000-04-30
  • 打赏
  • 举报
回复
un1,
我把你的整理了一下, 如下, 但不灵, 读写都是怪字符, 你试试.
另外我是想存进去时把文件里原来的内容清掉, 怎么办?

Sub SaveFile(str As String, sFileName As String)
Dim l As Long
Dim lf As Long
Dim bt() As Byte

On Error GoTo ErrorHandle

l = FreeFile()
Open sFileName For Binary As #l
lf = l
bt = str
Put #lf, , bt
Erase bt
Close #lf
lf = 0

Exit Sub

ErrorHandle:
MsgBox Err.Description & Chr(13) & "SaveFile"

End Sub
Function LoadFile(sFileName As String) As String
Dim lc As Long
Dim l As Long
Dim lf As Long
Dim bt() As Byte

On Error GoTo ErrorHandle

lc = FileLen(sFileName)
If lc <= 0 Then Err.Raise 321
l = FreeFile()
Open sFileName For Binary As #l
lf = l
ReDim bt(lc - 1)
Get #lf, , bt
Close #lf
lf = 0
LoadFile = bt
Erase bt

Exit Function

ErrorHandle:
MsgBox Err.Description & Chr(13) & "LoadFile"

End Function
Un1 2000-04-29
  • 打赏
  • 举报
回复
VB5以上都不应该有这个问题,因为VB的String没有64K限制:

Write:

...

Dim s As String
s = String(65536& * 2, "A")

Dim l As Long
l = FreeFile()
Open FilePath For Binary As #l

Dim lf As Long
lf = l

On Error Goto ErrorHandle

Dim bt() As Byte
bt = s

Put #lf,,bt

Erase bt

Close #lf
lf = 0

...


Read:
...

Dim lc As Long
lc = FileLen(FilePath)

If lc<=0 Then Err.Raise 321

Dim l As Long
l = FreeFile()
Open FilePath For Binary As #l

Dim lf As Long
lf = l

On Error Goto ErrorHandle

Dim bt() As Byte
Redim bt(lc-1)

Get #lf,,bt
Close #lf
lf = 0

Dim s As String
s = bt

Erase bt

...

'now s restored
Un1 2000-04-29
  • 打赏
  • 举报
回复
VB5以上都不应该有这个问题,因为VB的String没有64K限制:

Write:

...

Dim s As String
s = String(65536& * 2, "A")

Dim l As Long
l = FreeFile()
Open FilePath For Binary As #l

Dim lf As Long
lf = l

On Error Goto ErrorHandle

Dim bt() As Byte
bt = s

Put #lf,,bt

Erase bt

Close #lf
lf = 0

...


Read:
...

Dim lc As Long
lc = FileLen(FilePath)

If lc<=0 Then Err.Raise

Dim l As Long
l = FreeFile()
Open FilePath For Binary As #l

Dim lf As Long
lf = l

On Error Goto ErrorHandle

Dim bt() As Byte
Redim bt(lc-1)

Get #lf,,bt
Close #lf
lf = 0

Dim s As String
s = bt

Erase bt

...

'now s restored

7,763

社区成员

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

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