您的位置:程序门 -> vb -> 基础类



vb读写文件的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb读写文件的问题
发表于:2008-01-15 14:48:59 楼主
写文件
    我要实现写文件的功能,功能如下:
   
    有10000组数据,有几个int类型的数据(假设六个),要将其写到   一个文件中去.每行一组数据,最终格式如下.  
    数据1           数据2       数据3     数据4     数据5   数据6
    数据1           数据2       数据3     数据4     数据5   数据6
    .....................
    每个数据均采用3位(因为数据大小不超过1000)来显示,以方便对齐,数据间有1个空格,一组数据写完换行,下一行为另一组数据

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

 

 
   
   
发表于:2008-01-15 14:54:341楼 得分:0
刚学vb,要急用,只解决写入一行int型数据也给分,谢谢
发表于:2008-01-15 15:17:422楼 得分:0
假定输入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
发表于:2008-01-15 15:19:583楼 得分:0

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

假定输入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
发表于:2008-01-15 15:23:284楼 得分:0
不好意思,又错了,看以下

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
发表于:2008-01-15 15:35:355楼 得分:0
谢谢哈,我试试
发表于:2008-01-15 15:45:576楼 得分:0
这个模块如何调用啊?刚学,不懂
你这代码是不是vbscript的代码啊?

发表于:2008-01-15 15:56:127楼 得分:0
是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
发表于:2008-01-16 09:25:318楼 得分:0
感觉还是有问题啊,怎么里面都是script之类的代码,一般的都是open   ,put   ,set   ,write之类的
发表于:2008-01-16 15:29:299楼 得分:0
...........vbscript的用法?
我这样是可以的,测试通过

vbscript code
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'
发表于:2008-01-17 08:18:0410楼 得分:0
在vb中直接用,不是vbscript,不过在vbscript中也是这么用,你不知道filesyatemobject?
发表于:2008-01-17 08:40:3311楼 得分:0
仅仅是写   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


快速检索

最新资讯
热门点击