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
'定义一计数器来处理不就完了?! 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
你意思是是判断这个计数器nCounter的数来执行相关操作吧。 就是用 if nCounter = 1 then 相关操作.... end ifif nCounter = 2 then 相关操作.... end if这样的话那不是如果文本有多少行那就要定义多少个if或switch case?如果能让文本直接操完成一条记录的相关操作后,自动移到下一条记录在进行相关操作,是不是会更好一些?
Line Input本身就是读取一行后自动移到下一行啦。 你每行的处理不同,当然要根据是哪一行判断处理,跟自动移行什么关系?
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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 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
nFile = FreeFile
Open 全路径文件名 For Input As #nFile
Do While Not EOF(nFile)
Line Input #nFile, strTmp 'strtmp为读取的一行字符
......
Loop
Close #nFile
能不能有一种方面像是操作数据库那样的,就像MoveNext那样直接移到下一条记录。
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
就是用
if nCounter = 1 then
相关操作....
end ifif nCounter = 2 then
相关操作....
end if这样的话那不是如果文本有多少行那就要定义多少个if或switch case?如果能让文本直接操完成一条记录的相关操作后,自动移到下一条记录在进行相关操作,是不是会更好一些?
你每行的处理不同,当然要根据是哪一行判断处理,跟自动移行什么关系?
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
1、将文件读入到一字节数组中(Get #1, , arrb )
2、将数组转为字符串(strconv(arrb,vbunicode))
3、根据回车换行符(vbcrlf))来分解该字符串到一数组s()(s()=split(strconv(arrb,vbunicode),vbcrlf)
)
4、数组s()每个元素就是文本每行的字符串
5、对数组s循环,对每行进行操作 call 相关操作(i)
其中这个 相关操作(i)仅限于操作与行有规律的情况
' 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