读取cfd/文本文件并删除空行 >我试图在VB6.0中创建一个函数来读取CFD文件中的行,然后从该文件中删除空白行?谁能帮我一下吗

读取cfd/文本文件并删除空行 >我试图在VB6.0中创建一个函数来读取CFD文件中的行,然后从该文件中删除空白行?谁能帮我一下吗,vb6,Vb6,我想说的是 /The file is already read through a ReadStream /Once its read I would like to identify the blank rows / delete them.take the blank rows 我尝试了很多不同的方法,这里是最后一种: Do Until Len(msLineRecord) = ReadStream.AtEndOfStream msLineRecord = Replace(ms

我想说的是

/The file is already read through a ReadStream
/Once its read I would like to identify the blank rows 
/ delete them.take the blank rows 

我尝试了很多不同的方法,这里是最后一种:

Do Until Len(msLineRecord) = ReadStream.AtEndOfStream 
    msLineRecord = Replace(msLineRecord, vbNewLine & vbNewLine, vbNewLine) 
    msLineRecord = Len(msLineRecord)
Loop


似乎您正在使用FileSystemObject打开并读取您的文件,因此我假设cfd文件是纯文本文件。我不包括错误处理。你应该能做到

Dim fso As New FileSystemObject
Dim fsoSourceStream As TextStream
Dim fsoTempStream As TextStream
Dim fsoFile As File
Dim strLine As String

' Create a temporary text file, and return a reference to a TextStream
Set fsoTempStream = fso.CreateTextFile("some_path\temporary.txt", True) ' the True parameter overwrites the file if it already exists

' Open the source file for reading and return a reference to the TextStream
Set fsoFile = fso.GetFile("some_path\my_file.cfd")
Set fsoSourceStream = fsoFile.OpenAsTextStream(ForReading)

' Loop through the lines writing the lines that are not blank to the temp file
Do While Not fsoSourceStream.AtEndOfStream
    strLine = fsoSourceStream.ReadLine
    If Len(strLine) > 0 Then
        fsoTempStream.WriteLine strLine
    End If
Loop

fsoSourceStream.Close
fsoTempStream.Close

fso.DeleteFile("some_path\my_file.cfd") ' Delete the source file
' Rename the temporary file to the source file name
Set fsoFile = fso.GetFile("some_path\temporary.txt")
fsoFile.Name = "some_path\my_file.cfd"

' Clean up
Set fso = Nothing
Set fsoFile = Nothing
Set fsoSourceStream = Nothing
Set fsoTempStream = Nothing

打开一个临时文件,当您从源中读取行时,如果它们不是空的,则将它们写入临时文件。完成后,请删除原始文件并将临时文件重命名为原始文件名。谢谢,请同时向我显示代码。不,您没有提供任何代码供我使用。我尝试了许多不同的方法。这里是最后一种方法:Do Until Len(msLineRecord)=ReadStream.AtEndOfStream msLineRecord=Replace(msLineRecord,vbNewLine&vbNewLine,vbNewLine)msLineRecord=Len(msLineRecord)LoopDo直到ReadStream.AtEndOfStream如果LenB(Trim$(strLine))=0,则如果未找到空白行,则打印#2,strLine bFoundBlankLine=True End If Else打印#2,strLine End If Loop Close ff1 Close ff2 Kill“C:\temp\blankline.txt”Name“C:\temp\MyTemp.tmp”为“C:\temp\blankline.txt”谢谢。这太棒了
Dim fso As New FileSystemObject
Dim fsoSourceStream As TextStream
Dim fsoTempStream As TextStream
Dim fsoFile As File
Dim strLine As String

' Create a temporary text file, and return a reference to a TextStream
Set fsoTempStream = fso.CreateTextFile("some_path\temporary.txt", True) ' the True parameter overwrites the file if it already exists

' Open the source file for reading and return a reference to the TextStream
Set fsoFile = fso.GetFile("some_path\my_file.cfd")
Set fsoSourceStream = fsoFile.OpenAsTextStream(ForReading)

' Loop through the lines writing the lines that are not blank to the temp file
Do While Not fsoSourceStream.AtEndOfStream
    strLine = fsoSourceStream.ReadLine
    If Len(strLine) > 0 Then
        fsoTempStream.WriteLine strLine
    End If
Loop

fsoSourceStream.Close
fsoTempStream.Close

fso.DeleteFile("some_path\my_file.cfd") ' Delete the source file
' Rename the temporary file to the source file name
Set fsoFile = fso.GetFile("some_path\temporary.txt")
fsoFile.Name = "some_path\my_file.cfd"

' Clean up
Set fso = Nothing
Set fsoFile = Nothing
Set fsoSourceStream = Nothing
Set fsoTempStream = Nothing