我是新手,问一个文本操作方面的简单问题

flash88 2005-10-12 11:05:25
我现在要对一个文本进行操作,从第一行开始读。每读一行就进行相关的操作,然后在读下一行,依此类推,只到读完全部文本。谢谢各位最好能给点代码参考。
...全文
148 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
winehero 2005-10-12
  • 打赏
  • 举报
回复
可以,用以上那个方法就行
flash88 2005-10-12
  • 打赏
  • 举报
回复
我想只一个流程走下来如:
打开文本
读取第一行
执行相关操作
读第二行
执行相关操作
读第三行
执行相关操作
只到读完
这样可以吗?
winehero 2005-10-12
  • 打赏
  • 举报
回复
Line Input本身就是读取一行后自动移到下一行啦。
你每行的处理不同,当然要根据是哪一行判断处理,跟自动移行什么关系?
flash88 2005-10-12
  • 打赏
  • 举报
回复
你意思是是判断这个计数器nCounter的数来执行相关操作吧。
就是用
if nCounter = 1 then
相关操作....
end if

if nCounter = 2 then
相关操作....
end if

这样的话那不是如果文本有多少行那就要定义多少个if或switch case?如果能让文本直接操完成一条记录的相关操作后,自动移到下一条记录在进行相关操作,是不是会更好一些?
winehero 2005-10-12
  • 打赏
  • 举报
回复
'定义一计数器来处理不就完了?!
dim nCounter as Long ' 计数器
Dim nFile as integer, strTmp as string
nFile = FreeFile
Open 全路径文件名 For Input As #nFile
Do While Not EOF(nFile)
Line Input #nFile, strTmp 'strtmp为读取的一行字符
nCounter=nCounter+1 'nCounter即第几行

......
Loop

Close #nFile
flash88 2005-10-12
  • 打赏
  • 举报
回复
先谢谢上面的了,代码可以用。但是这样的话,所有读出行的操作都相同了,我每读一行操作是不同的。也就是说我读第一行时是写入到文件1里面,我读第二行时是写入到文件2里面。
能不能有一种方面像是操作数据库那样的,就像MoveNext那样直接移到下一条记录。
winehero 2005-10-12
  • 打赏
  • 举报
回复
Dim nFile as integer, strTmp as string
nFile = FreeFile
Open 全路径文件名 For Input As #nFile
Do While Not EOF(nFile)
Line Input #nFile, strTmp 'strtmp为读取的一行字符
......
Loop

Close #nFile
flash88 2005-10-12
  • 打赏
  • 举报
回复
谢谢各位了
crycoming 2005-10-12
  • 打赏
  • 举报
回复
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetLyrics
' Purpose:
' Displays the contents of the lyrics files.
' Demonstrates the following
' - FileSystemObject.OpenTextFile
' - FileSystemObject.GetFile
' - TextStream.ReadAll
' - TextStream.Close
' - File.OpenAsTextStream
' - TextStream.AtEndOfStream
' - TextStream.ReadLine
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Function GetLyrics(FSO)

Dim TextStream
Dim S
Dim File

' There are several ways to open a text file, and several
' ways to read the data out of a file. Here's two ways
' to do each:

Set TextStream = FSO.OpenTextFile(TestFilePath & "\Beatles\OctopusGarden.txt", OpenFileForReading)

S = TextStream.ReadAll & NewLine & NewLine
TextStream.Close

Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
Set TextStream = File.OpenAsTextStream(OpenFileForReading)
Do While Not TextStream.AtEndOfStream
S = S & TextStream.ReadLine & NewLine
Loop
TextStream.Close

GetLyrics = S

End Function

flash88 2005-10-12
  • 打赏
  • 举报
回复
哦,大概明白了,谢谢了啊。
winehero 2005-10-12
  • 打赏
  • 举报
回复
northwolves(狼行天下)的处理是
1、将文件读入到一字节数组中(Get #1, , arrb 。。。)
2、将数组转为字符串(strconv(arrb,vbunicode))
3、根据回车换行符(vbcrlf))来分解该字符串到一数组s()(s()=split(strconv(arrb,vbunicode),vbcrlf)
)
4、数组s()每个元素就是文本每行的字符串
5、对数组s循环,对每行进行操作 call 相关操作(i)
其中这个 相关操作(i)仅限于操作与行有规律的情况
flash88 2005-10-12
  • 打赏
  • 举报
回复
我是新手狼行天下的代码看不懂,能不能解释下啊。
northwolves 2005-10-12
  • 打赏
  • 举报
回复
Dim arrb() as byte,s() as string,i as long
Open "c:\xxx.txt" For Binary As #1
ReDim arrb(LOF(1)) As Byte
Get #1, , arrb
Close 1
erase arrb
s()=split(strconv(arrb,vbunicode),vbcrlf)
for i=0 to ubound(s)
call 相关操作(i)
next
erase s

7,763

社区成员

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

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