简体   繁体   English

从文件读取文本

[英]Reading text from a file

I am writing some code to search a file at the start of the file until a string is found. 我正在编写一些代码以在文件的开头搜索文件,直到找到字符串为止。

Here is the code that works: 这是有效的代码:

    Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
        Dim result As String = String.Empty
        Dim sb As New System.Text.StringBuilder()
        Dim previousLength As Integer = 0
        Using reader As New IO.StreamReader(txtFile)
            'Read the file 1 char at a time and append it to sb
            While reader.Peek >= 0
                sb.Append(Convert.ToChar(reader.Read))
                'store the current length of sb
                previousLength = sb.Length
                'attemp to replace the search string with an empty string. If the length changed after the replacement
                'then the following conditions must be true:
                ' 1. we have found the search string in sb.
                ' 2. the search string was at the end of sb.
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    'since we have found the search string,  exit the loop and return the string currently in sb.
                    result = sb.ToString()
                    Exit While
                End If
            End While
        End Using
        Return result
    End Function

I am wanting to edit thsi code such that I can choose a start position in the file to start searching. 我想编辑此代码,以便可以选择文件中的开始位置以开始搜索。

Here is the code that I am working on: 这是我正在处理的代码:

    Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim previousLength As Integer = 0
    Using reader As New IO.StreamReader(txtFile)
        'Read the file 1 char at a time and append it to sb
        While reader.Peek >= integerStartPosition
            sb.Append(Convert.ToChar(reader.Read))
            'store the current length of sb
            previousLength = sb.Length
            'attemp to replace the search string with an empty string. If the length changed after the replacement
            'then the following conditions must be true:
            ' 1. we have found the search string in sb.
            ' 2. the search string was at the end of sb.
            sb.Replace(searchString, String.Empty)
            If sb.Length < previousLength Then
                'since we have found the search string,  exit the loop and return the string currently in sb.
                result = sb.ToString()
                Exit While
            End If
        End While
    End Using
    Return result
End Function

Can I please have some help to get this working? 我可以帮忙吗?

FINAL CODE 最终密码

Public Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String, integerStartPosition As Integer) As String
    Dim result As String = String.Empty
    Dim sb As New System.Text.StringBuilder()
    Dim tmpString As String = ""
    Dim previousLength As Integer = -1
    Dim integerCurrentPosition As Integer 
    Using reader As New IO.StreamReader(txtFile)
        While reader.Peek >= 0

            integerCurrentPosition += 1

            tmpString = Convert.ToChar(reader.Read)

            If integerCurrentPosition > integerStartPosition Then
                sb.Append(tmpString)
                previousLength = sb.Length
                sb.Replace(searchString, String.Empty)
                If sb.Length < previousLength Then
                    result = sb.ToString()
                    Exit While
                End If
            End If

        End While
    End Using
    Return result
End Function

You should put position checking inside the while loop. 您应该将位置检查放在while循环中。 Here's a simple example how to do it. 这是一个简单的示例。

Private Function ReadTextFileUpto(ByVal txtFile As String, ByVal searchString As String) As String
            Dim result As String = String.Empty
            Dim sb As New System.Text.StringBuilder()
            Dim previousLength As Integer = 0
            Dim currentPosition as Integer = 0

            Using reader As New IO.StreamReader(txtFile)
                'Read the file 1 char at a time and append it to sb
                While reader.Peek >= 0
                    If currentPosition < integerStartPosition Then
                       Continue While
                    End if 

                    sb.Append(Convert.ToChar(reader.Read))
                    'store the current length of sb
                    previousLength = sb.Length
                    'attemp to replace the search string with an empty string. If the length changed after the replacement
                    'then the following conditions must be true:
                    ' 1. we have found the search string in sb.
                    ' 2. the search string was at the end of sb.
                    sb.Replace(searchString, String.Empty)
                    If sb.Length < previousLength Then
                        'since we have found the search string,  exit the loop and return the string currently in sb.
                        result = sb.ToString()
                        Exit While
                    End If

                    currentPosition = currentPosition + 1
                End While
            End Using
            Return result
        End Function

UPDATE: 更新:

Or you can try StreamReader.Read Method (Char[], Int32, Int32) method to read stream into array, specifying starting position. 或者,您可以尝试StreamReader.Read方法(Char [],Int32,Int32)方法将流读取到数组中,并指定起始位置。

Reads a maximum of count characters from the current stream into buffer, beginning at index. 从索引开始,将当前计数的最大字符数读入缓冲区。

For more info check tutorial 有关更多信息,请查看教程

StreamReader StreamReader

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM