首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取cfd/文本文件并删除空行

读取cfd/文本文件并删除空行
EN

Stack Overflow用户
提问于 2014-02-27 03:00:34
回答 1查看 550关注 0票数 1

您好,我正试图在VB6.0中创建一个函数来从cfd文件中读取行,然后从该文件中删除空行?有谁能帮我一下吗?

我想说的是。

代码语言:javascript
运行
复制
/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 

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

代码语言:javascript
运行
复制
Do Until Len(msLineRecord) = ReadStream.AtEndOfStream 
    msLineRecord = Replace(msLineRecord, vbNewLine & vbNewLine, vbNewLine) 
    msLineRecord = Len(msLineRecord)
Loop

也是

代码语言:javascript
运行
复制
Do Until ReadStream.AtEndOfStream
    If LenB(Trim$(strLine)) = 0 Then
        If Not bFoundBlankLine Then
            Print #2, strLine bFoundBlankLine = True
        End If
    Else
        Print #2, strLine
    End If
Loop
Close ff1
Close ff2
Kill "C:\temp\blank lines.txt"
Name "C:\temp\MyTemp.tmp" As "C:\temp\blank lines.txt"
EN

回答 1

Stack Overflow用户

发布于 2014-02-27 05:43:04

您似乎正在使用FileSystemObject打开和读取您的文件,所以我将假设cfd文件是一个纯文本文件。我不包括错误处理。您应该能够做到这一点。

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22050721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档