VB读写文件的问题

hzhiyang84 2008-01-15 02:48:59
写文件
我要实现写文件的功能,功能如下:

有10000组数据,有几个int类型的数据(假设六个),要将其写到 一个文件中去.每行一组数据,最终格式如下.
数据1 数据2 数据3 数据4 数据5 数据6
数据1 数据2 数据3 数据4 数据5 数据6
.....................
每个数据均采用3位(因为数据大小不超过1000)来显示,以方便对齐,数据间有1个空格,一组数据写完换行,下一行为另一组数据

创建文件时以运行程序的起始时间命名,如本次运行时间为14:49 则文件命名 output1449.txt






...全文
208 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2008-01-17
  • 打赏
  • 举报
回复
仅仅是写 txt 文件没有必要动用 FileSyatemObject:

Dim i As Integer, j As Integer, n As Integer, strLine As String

Open App.Path & "\output" & Format(Now(), "HHnn") & ".txt" For Output As #1

For i = 0 To Ubound(Data) Step 6
strLine = ""
n = IIf(i < Ubound(Data) - 6, 5, Ubound(Data) - i)
For j = 0 To n
strLine = IIF(strLine > "", " ", "") & Format(Data(i + j), "00#")
Next j
Print #1, strLine
Next i

Close #1
z_wenqian 2008-01-17
  • 打赏
  • 举报
回复
在vb中直接用,不是vbscript,不过在vbscript中也是这么用,你不知道FileSyatemObject?
hzhiyang84 2008-01-16
  • 打赏
  • 举报
回复
感觉还是有问题啊,怎么里面都是script之类的代码,一般的都是open ,put ,set ,write之类的
awperpvip 2008-01-16
  • 打赏
  • 举报
回复
...........Vbscript的用法?
我这样是可以的,测试通过

Private Sub Command1_Click()
a
End Sub
Sub a()
Dim filename As String
Dim FSO As FileSystemObject
Dim FS As TextStream
Dim tmpStr As String

filename = App.Path & "\" & "output" & Format(Now, "hhnn") & ".txt"

Set FSO = New FileSystemObject
Set FS = FSO.OpenTextFile(filename, ForWriting, True)

FS.Write "123 456 789 987 654 321"
FS.Close

Set FS = Nothing
Set FSO = Nothing

End Sub


注意添加相关引用 'Mircosoft Scripting Runtime'
z_wenqian 2008-01-15
  • 打赏
  • 举报
回复
是vb,将代码中的filename加上路径名,在另一个过程中调用就行了,参考以下:
filename = "d:\test\output" & Format(Now, "hhnn") & ".txt"

假定在主窗口上有一命令按钮Command1
Private Sub Command1_Click()
dim data(0 to 10000) as integer
data(0)=...
data(1)=...
.
.
call a(data)
end sub
hzhiyang84 2008-01-15
  • 打赏
  • 举报
回复
这个模块如何调用啊?刚学,不懂
你这代码是不是VBScript的代码啊?

hzhiyang84 2008-01-15
  • 打赏
  • 举报
回复
谢谢哈,我试试
z_wenqian 2008-01-15
  • 打赏
  • 举报
回复
不好意思,又错了,看以下

Sub a(ByRef data() As Integer)
Dim filename As String
Dim i As Long, j As Long, s As String
Dim l As Long
l = UBound(data)
filename = "output" & Format(Now, "hhnn") & ".txt"
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Set Fw = Fso.CreateTextFile(filename, True)
Do
s = ""
For i = 0 To 5
If 6 * j + i > l Then Exit For
s = s & data(j * 6 + i) & " "
Next i
Fw.WriteLine s
j = j + 1
If 6 * j > l Then Exit Do
Loop
Fw.Close
End Sub
z_wenqian 2008-01-15
  • 打赏
  • 举报
回复

刚才发的有问题,以此为准

假定输入10000个数据在data()中
Sub a(ByRef data() As Integer)
Dim filename As String
Dim i As Long, j As Long, s As String
On Error GoTo lExit
filename = "output" & Format(Now, "hhnn") & ".txt"
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Set Fw = Fso.CreateTextFile(filename, True)
Do
s = ""
For i = 0 To 5
s = s & data(j * 6 + i) & " "
Next i
Fw.WriteLine s
j = j + 1
Loop
lExit:
Fw.Close
End Sub
z_wenqian 2008-01-15
  • 打赏
  • 举报
回复
假定输入10000个数据在data()中
Sub a(data() As Integer)
Dim filename As String
Dim i As Long, j As Long, s As String
On Error Resume Next
filename = "output" & Format(Now, "hhnn") & ".txt"
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Set Fw = Fso.CreateTextFile(filename, True)
Do
s = ""
For i = 0 To 5
s = s & data(j * 6 + i) & " "
Next i
Fw.WriteLine s
j = j + 1
Loop Until fr.AtEndOfStream
Fw.Close
End Sub
hzhiyang84 2008-01-15
  • 打赏
  • 举报
回复
刚学VB,要急用,只解决写入一行int型数据也给分,谢谢

7,763

社区成员

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

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