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



请教vb6.0对txt文件进行读写操作问题


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


请教vb6.0对txt文件进行读写操作问题[已结贴,结贴人:jhzcode01]
发表于:2007-02-08 20:49:01 楼主
有两个txt文件a和b,从a中读取数据并进行判断,如果符合判断条件则写入b文件中,我用了以下的程序,不进行if判断时是可以正常读写操作的,为何加入if语句后在b文件中就没有输出结果了呢?

dim   tstr   as   string
dim   intext   as   string

private   sub   command1_click()
        open   ( "c:\test1.txt ")   for   input   as   #1
                do   while   not   eof(1)
                        line   input   #1,   intext
                        tstr   =   tstr   +   intext   +   chr(13)   +   chr(10)
                loop
                close   #1
       
        open   ( "c:\test2.txt ")   for   output   as   #2
        if   trim(tstr)   =   trim( "asd ")   then         '对读取的数据进行判断
                print   #2,   tstr     '如果符合判断条件则在test2中加入数据
        else
                close   #2
        end   if
        text1.text   =   tstr
end   sub
发表于:2007-02-08 20:57:101楼 得分:0
text1.text   是什么?  

将     tstr   =   tstr   +   intext   +   chr(13)   +   chr(10)     和   trim( "asd ")       比较,肯定不是你想要的。

去掉   不显示的字符,应该可以了。

另外,chr(13)   +   chr(10)     =>   vbcrlf  
   

发表于:2007-02-08 21:08:142楼 得分:5
open   ( "c:\test2.txt ")   for   output   as   #2
        if   trim(tstr)   =   trim( "asd ")   then         '对读取的数据进行判断
                print   #2,   tstr     '如果符合判断条件则在test2中加入数据
        else
                close   #2
        end   if
-------------
如果if语句条件成立,那你什么时候关闭#2文件啊
发表于:2007-02-08 22:11:393楼 得分:5
改改试下
if   tstr= "asd "   then
open   ( "c:\text2.txt ")   for   output   as   #2
print#2,tstr
close#2
else
exit   sub
endif
发表于:2007-02-08 22:15:004楼 得分:0
你这个asd怎么来的???
发表于:2007-02-08 22:49:335楼 得分:0
听取大家的宝贵意见后,我对代码进行了修改(代码如下),已可以根据判断的值进行读写操作,但问题又来了,只能读取a文件中的第一行和在b文件中的第一行进行写操作。问题:如何让读写文件的指针在一次读写完成后指向下一行呢?
dim   tstr   as   string
dim   intext   as   string

private   sub   command1_click()
        open   ( "c:\test1.txt ")   for   input   as   #1
                if   not   eof(1)   then
                        line   input   #1,   intext
                        'tstr   =   tstr   +   intext
                        close   #1
                end   if
        open   ( "c:\test2.txt ")   for   output   as   #2
        if   intext   =   "asd "   then         '对读取的数据进行判断
                print   #2,   intext       '如果符合判断条件则在test2中加入数据 "
        end   if
                close   #2
end   sub
发表于:2007-02-08 22:56:516楼 得分:10
line   input   之后,自动到下一行。

print   后自动到下一行。


dim   intext   as   string

private   sub   command1_click()
        open   "c:\test1.txt "   for   input     as   #1
        open   "c:\test2.txt "   for   output   as   #2
       
        do   while   not   eof(1)
                line   input   #1,   intext

                if   intext   =   "asd "   then         '对读取的数据进行判断
                        print   #2,   intext       '如果符合判断条件则在test2中加入数据 "
                end   if
        loop
       
        close   #2
        close   #1
end   sub

   

发表于:2007-02-09 00:10:237楼 得分:0
已经成功了,谢谢cangwu_lee(橙子)回复,谢谢!
发表于:2007-02-09 19:44:358楼 得分:0
终于得到自己想要的结果了,谢谢上面对面几位大侠的大力帮助!下面贴出的就是完整的程序,希望对和我一样被类似问题困扰的朋友有所帮助。
dim   str1   as   string
dim   str2   as   string

private   sub   command1_click()
        open   "c:\test1.txt "   for   input   as   #1           '打开文件
        open   "c:\test3.txt "   for   output   as   #3                 '打开文件
       
        do   while   not   eof(1)           '如果指针不是指向文件结尾,则执行循环
                line   input   #1,   str1           '读取文件内容并将值赋给变量str1
                call   compare                 '调用compare过程
        loop
       
        close   #1
        close   #3
end   sub

private   sub   command2_click()
unload   me
end   sub
private   sub   compare()
                open   "c:\test2.txt "   for   input   as   #2
               
                        do   while   not   eof(2)
                                line   input   #2,   str2
                                        if   strcomp(trim(str1),   trim(str2),   vbtextcompare)   =   ture   then       '调用strcomp函数,对读取的数据进行判断
                                                print   #3,   str2     '如果符合判断条件则在test3中写入数据
                                        end   if
                        loop
                       
                close   #2
end   sub
发表于:2007-09-06 11:26:479楼 得分:0
public   function   downloadfile(url   as   string,   localfilename   as   string)   as   boolean   '下载文件


快速检索

最新资讯
热门点击