当前位置:文档之家› vb操作txt(VB operation TXT)

vb操作txt(VB operation TXT)

vb操作txt(VB operation TXT)细胞(1,1)代表A1细胞(2,C)代表C2 = COUNTIF(“”)+ COUNTIF(“”)+ COUNTIF(“”)1。

怎么通过代码创建一个文本文件,并读取,更新内容以上为随即方式打开的文本文件昏暗的gfilenum为整数gfilenum = FreeFile随机gfilenum len = 3打开“文件路径及文件名”“以随即方式打开一文件如果文件不存在就新建用得到# gfilenum,记录在文件中位置,要放取得的数据的变量”读取操作用把# gfilenum,记录在文件中位置,要放着要写入数据的变量”写操作近# filenum”关闭文件以下为顺序方式打开的文件昏暗的gfilenum为整数gfilenum = FreeFile打开输出”文件路径及文件名”gfilenum”以写入方式打开文本打印# gfilenum,要写入的文本写#文件号,要写入的文本昏暗的gfilenum为整数gfilenum = FreeFile打开“文件路径及文件名“输入”以读出方式打开文本gfilenum输入# gfilenum,用来放读取的内容的内存变量名还可用线路输入#,等读取更详细的查MSDN()2。

VB读取文本文件时,调用对象中使用报错了文本中去。

如果是集F = fs。

中去(“E:\表格\痕迹。

txt”,读书,真的,TristateUseDefault)出现错误:运行时错误格”:无效的过程调用或参数如果是集F = fs。

中去(“E:\表格\痕迹。

txt)”则不会报错。

用文件对象啊filesystemcontrol操作就好了啊再加上一个文本读取文件第一行就好了啊。

3。

VB通过FileSystemObject,可以读取文本文件(.txt)。

对于。

蝙蝠文件,VB可否直接读取?蝙蝠也属于文本类文件可以读取不用FSO也行:把文本文件内容读取文本框:朦胧的TempFile As Long昏暗的loadbytes()字节tempfile = FreeFile二开文件名作为# tempfileRedim LoadBytes(1~LOF(TempFile))为字节得到# tempfile,,LoadBytes关闭tempfiletext1。

文本= StrConv(loadbytes,vbunicode)把TextBox内容写入文本文件:朦胧的TempFile As Long昏暗的savebytes()字节savebytes = StrConv(text1。

文本,vbfromunicode)tempfile = FreeFile二开文件名作为# tempfile把# tempfile,,SaveBytes关闭tempfile4。

打开文本文件打开app.path &”/你的文本文件名.txt”#输出为1 名称= text1.text消息。

手机= text2.text消息。

.......................邮政编码= text5.text消息。

把# 1,我的信息”(此处i = 1,如要多次写入,可用循环设置我的值)近1 #“写入的同时即已保存查询的话就取出文本里的内容,用instr()函数就可实现了5。

查找vbcrlf、vblf换行标记6。

怎样读取一个文本文件的全部内容昏暗的lenfile为整数昏暗的filenum为整数filenum = freefile()打开“文件.dat”输入filenumlenfile = LOF(# filenum)strfile =输入(lenfile,# filenum)的将所有数据放入变量strfile中近filenum私有子form_load()Const ForReading = 1,= 2等模糊FSO昏暗的skiplineinfile作为字符串设置FSO = CreateObject(“脚本。

FileSystemObject”)集F = FSO。

中去(“C:\测试文件.txt”,写作,真的)写“你好,世界!”与vbcrlf和VB脚本很有趣!”集F = FSO。

中去(“C:\测试文件.txt”,ForReading)skiplineinfile = f.readall打印skiplineinfile调试。

端子7把文本文件内容读取文本框:朦胧的TempFile As Long昏暗的loadbytes()字节tempfile = FreeFile二开文件名作为# tempfileRedim LoadBytes(1~LOF(TempFile))为字节得到# tempfile,,LoadBytes关闭tempfiletext1。

文本= StrConv(loadbytes,vbunicode)8。

把TextBox内容写入文本文件:朦胧的TempFile As Long昏暗的savebytes()字节savebytes = StrConv(text1。

文本,vbfromunicode)tempfile = FreeFile二开文件名作为# tempfile把# tempfile,,SaveBytes关闭tempfile9。

已知文本文件,要从中搜索一段特定的字符串信息。

如搜索[ magic_databases ],该怎么做?将字符串作为字符串输入# 1开commondialog1.filename输入# 1,STR在哪里作为整数=仪器(STR,“magic_databases”)10。

线路输入#语句示例本示例使用线路输入#语句从顺序文件中读入一行数据,并将该行数据赋予一个变量文件内含数行文本数据本示例假设测试文件。

昏暗的文本行打开“测试文件”,输入为1的打开文件#。

而不是EOF(1)的循环至文件尾。

线路输入# 1,文本行的读入一行数据并将其赋予某变量。

打印文本行的在立即窗口中显示数据调试。

环近1的关闭文件#。

11如何操作”文本文件”。

FSO(FileSystemObject)对象集中的文件集合里有readline方法。

对象的昏暗fs作为对象昏暗的文本行的字符串设置FS = CreateObject(“脚本。

FileSystemObject”)套= fs。

中去(APP,路径+“\\ Myfile. AAA”)/ / myfile.aaa是一个文本文件textling = of.readline14 /如果你要读取第行,可以用一个循环,跳过前面的13行,如下模糊为整数0 = 12of.skip下一个/ /然后再读取12。

搜索了一下,大家主要用两种方法对文本文件加密。

1。

对文本文件的ASC码加减2。

用异或函数我发现都有问题举例1。

代码公共职能nnnn(ByVal为字符串)作为字符串的解密下次继续出错以字符串为单位,以字节为单位,以字节为透镜镜头=镜头(S)对于i = 1到镜头SS = SS和CHR(ASC(MID(S,I,1))-镜头-我)下一个nnnn = SS端功能公共职能mmmm(ByVal为字符串)作为字符串的加密下次继续出错以字符串为单位,以字节为单位,以字节为透镜镜头=镜头(S)对于i = 1到镜头SS = SS和CHR(ASC(MID(S,I,1))+镜头+ 1)下一个嗯= SS端功能当文件中有”!~”时出错异或加密暗淡我一样长我= 1,UBound(loadbytes)LoadBytes(我)= LoadBytes(我)异或和HFF下一个我13。

怎么判断一个文本文件是否打开?判断如果没打开就删除该文本文件!要用到FileSystemObject对象的找到和DeleteFile方法如果DIR(来)=“”然后退出文件号=FreeFile”取得未使用的文件号。

错误程序的打开错误处理程序。

Open MyFile #输出文件号”打开输出文件。

杀“个”的试图删除已打开的文件。

退出的退出程序,以避免进入错误处理程序。

ErrorHandler:“错误处理程序。

选择的案例数量的检查错误代号犯错。

案例55的发生”文件已打开”的错误。

近#文件号”关闭已打开的文件。

其他案件“处理其他错误状态。

..最后选择简历的将执行返回到发生错误的语句。

14。

打开一个文本文件怎样最快?变长为长,TS为字符串FN = FreeFile打开“C:\测试。

txt”二进制# FNTS = StrConv(InputB(LOF(FN),# FN),vbunicode)text1 = TS近# FN变长为长,TS为字符串FN = FreeFile打开“C:\测试。

txt”二进制# FNTS =输入(LOF(FN),# FN)text1 = TS近# FN15。

怎么判断文本文件读取到了最后一行?用fsotextstream atendofstream可以判断。

16。

如何在文本文件中插入一行字串?用打开附加的方法只能加在最后面就用两个文件合并好了壳”命令/ C复制aa.txt + bb.txt cc.txt”,vbhide但我觉的不是太好,你自己看看对你有没有帮助吧。

17。

打开“测试文件.txt”输出为1的打开输出文件#。

写# 1,“Hello World”、“写入以逗号隔开的数据。

写# 1,”写入空白行。

近1的关闭文件#请问怎样在原先个.txt增加Hello World(即不删除原先的内容)打开“测试文件.txt”#追加为1写# 1,“Hello World”,近1 #18。

如何实现对文本文件任意一行的读写?利用VB的文件系统对象(FSO)对象模型要想对任一行读写,不用将前面内容顺序读出,但要用FSO对象的方法移动文件指针,具体方法如下:1。

在VB编辑器中引用“微软脚本运行时”2。

昏暗的FSO作为新的定义FSO对象FileSystemObject昏暗的TS为文本的定义文本流“打开文件设置TS = FSO。

中去(“C:\测试。

txt”,等)之后即可用TS的方法进行操作,可参见MSDN,以下简要说明:读数据有:读、readline、阅读写数据有:写、wirteblankline、wirteline移动文件指针有:跳过、跳过当前行操作完毕要关闭文件流TS。

Close19. write a new line at the end of the file:Dim tFileNumber As IntegerDim tWriteStr As String string 'to write.TFileNumber=FreeFileTWriteStr= what you needOpen pFileName For Append As #tFileNumber \'Append in order to write end of file folder. Or open in Output mode, and Seek (tFileNumberm, LOF (tFileNumber)) can also define pointers to the end of the file.Print #tFileNumber, tWriteStr 'into the file.Close #tFileNumberIf there is no line wrapping on the file, it will cause the first line you write to connect to the last line of the original file. The solution is:TWriteStr=Chr (13) & Chr (10) & tWriteStrOn the first line with this one, and this will lead to a newline symbol on the line.In addition, if you have 100 lines to write, then you write the file 100 times, it is better to merge the 100 linesinto a string, written together well. Write 100 lines longer than merge 100 lines written after a time to slow.At the same time, Open Close not to repeatedly write file. Open and Close have to re allocate the buffer, causing the program to decrease the speed of. As far as possible aftera Open, and then finished in Close, which can improve the efficiency.But it needs special attention! Before Close, if for some reason the illegal program closed, you may want to lose data. Because only after Close will really save all information buffers. And there is no Close file, there may be part of the content in the memory does not save, it is very dangerous. If you make a single operation in a short period of time, then write the process before Close. If a complicated operation takes a long time, will be repeated Open Close.Access operations between Open Close will achieve the fastest efficiency, while multiple Open Close gets the best security. Here's another trick: if you want to read a file and write a file at the same time, just open it in two different ways.Open "1.txt" For Input As #1Open "1.txt" For Output As #2Print #2, "little sister" write 1.txtSeek #1,1Line Input #1, Ld 1.txt 'readingSeek #1,1 'if you remove this sentence, you look at the time and read what? (there must be a mistake.)Line Input #1, LdClose #2Close #1Typical read files:Dim tFileNumber As Integer 'file No.Dim tLoadStr As String 'file read line variablesThe function tFileNumber=FreeFile \'FreeFile is getting the current idle file number. Automatic allocation of a file number. So this program can almost be compatible in all parts of the program.Open pFileName For Input As #tFileNumber \'Input for sequential read file.Line Input #tFileNumber, tLoadStr \'Line Input statement reads the whole row, otherwise, if only Input, the comma is also calculated a paragraph. If you want to read twelfth lines, do not know the location of the line under the premise of 12, it will read 12 times.Close #tFileNumberRead the file specified for file: read the biggest problem is to determine the file end. If more than the end of thefile, an error will occur. You can use a EOF function to determine whether the file pointer "at the end of file location.Method one (traditional method):Dim tFileNumber As IntegerDim tLoadStr As StringDim tEnd As Long 'for you to read the number of rows.Dim tAdd As Long 'notationTFileNumber=FreeFileTEnd=12 'if you want to read 12 linesOpen pFileName For Input As #tFileNumberdo until eof (tfilenumber) or tadd = men \ 'eof函数是判断是否已经到了结尾或到了你想找的行.line input # tfilenumber, tloadstrtadd = tadd + 1 \ '记数累加, 读一行加一.loopclose # tfilenumber方法二 (比较实用的):tfilenumber as integer dimtloadstr as string dimdim tend as long \ '你要读的行数.dim tadd as long \ '记数tfilenumber = freefilemen = 12 '' 假设你要读12行open pfilename for input as # tfilenumberfor tadd = 1 two menline input # tfilenumber, tloadstrif eof (tfilenumber) then exit for \ '到结尾则退出循环.syncclose # tfilenumber以上都是我在编程当中总结出来的非常惨痛的教训, 希望你不要步我后尘.20.用fso来读文本文件时怎样返回该文本文件的总行数?seen openfile = openfso.opentextfile(commondialog1.filename)static i as integerin = 0do while not openfile.atendoflineopenfile.readlinei = i + 1loopmsgbox "共" & & "行", vbokonly, "hello"21.treeview 能够生成两层以上的节点吗?private sub form _ load ()dim nodx as nodeseen nodx = treeview1.nodes.add (, "r", "root")seen nodx = treeview1.nodes.add ("r", tvwchild, "key1", "child")seen nodx = treeview1.nodes.add ("key1", tvwchild, "key2", "child _ child")nodx.ensurevisiblethan sub22.怎样获得一个文件夹里的文件数, 并且分别得到里面的文件名? dim stempt as stringdim sfilename (100) as the string \ '记录文件名dim inttotal as integer \ '文件总数inttotal = 0stempt = dir $(path & "1.5 *. *")do while stempt < > ""sfilename (inttotal) = stemptinttotal = inttotal + 1stempt = dir $loop23.取文件夹的文件名列表, 并按修改时间排序?\ '添加 [工程] - - [引用] "microsoft scripting runtime" option explicitprivate type typfliedatename as stringcreateddate as dateaccesseddate as datemodifieddate as datethan typeprivate myfiles () as typfliedate \ '包含文件的数组private sub command1 _ click ()dim n as integern = getfolderfiles ("d: 1.5", 1)than subprivate function getfolderfiles (path as string, orderby as integer) as integer\ 'path 文件夹路径\ 'orderby 排序依据按创建时间 1: 2: 3: 4: 按名称按修改时间按访问时间\ '返回文件夹中文件的个数\ '如果要计算子文件夹可以通过 fldr.subfolders 访问, 方法类似dim tmpfile as typfliedatedim n as integer, in the as integer, j as integerdim fso as new filesystemobjectdim fldr as folderdim fls as filesdim fl as file\ '读去文件seen fso = createobject ("scripting.filesystemobject")seen fldr = fso.getfolder(路径)FLS =科学文件集。

相关主题