代码之家  ›  专栏  ›  技术社区  ›  Doug

最有效的跳转文件和读取行的方法?

  •  2
  • Doug  · 技术社区  · 16 年前

    我想使用一个文件流并从文件的开头开始搜索,同时在文件中向前移动一次.01%的文件大小。

    所以我想在文件中寻找一个位置,阅读整行,如果它符合我的标准,我就完成了。如果没有,我会再找一个.01。

    C可以,但最好是vb.net。

    我以前在VB6中做过类似的事情…

                FileOpen(1, CurrentFullPath, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
            Dim FileLength As Long = LOF(1)
    
            For x As Single = 0.99 To 0 Step -0.01
                Seek(1, CInt(FileLength * x))
                Dim S As String = LineInput(1)
                S = LineInput(1)
                filePosition = Seek(1)
                If filePosition < 50000 Then
                    filePosition = 1
                    Exit For
                End If
                V = Split(S, ",")
                Dim MessageTime As Date = CDate(V(3) & " " & Mid$(V(4), 1, 8))
                Dim Diff As Integer = DateDiff(DateInterval.Minute, MessageTime, CDate(RequestedStartTime))
                If Diff >= 2 Then
                    Exit For
                End If
            Next
    

    但我不想使用fileopen,我想使用filestream。

    非常感谢您的帮助!

    4 回复  |  直到 9 年前
        1
  •  3
  •   bernhof    9 年前

    这或多或少是您的代码的直接转换,我们在其中使用filestream.position指定文件中要读取的位置:

    Using streamReader As System.IO.StreamReader = System.IO.File.OpenText(CurrentFullPath)
      For x As Single = 0.99 To 0 Step -0.01
        streamReader.BaseStream.Position = CLng(streamReader.BaseStream.Length * x)
        Dim S As String = streamReader.ReadLine()
        '... etc.
      Next
    End Using
    
        2
  •  1
  •   Joel Coehoorn    16 年前

    像这样的东西怎么样(C版):

    using (var file = System.IO.File.OpenText(filename))
    {
         while (!file.EndOfStream)
         {
              string line = file.ReadLine();
              //do your logic here
              //Logical test - if true, then break
         }
    }
    

    编辑:这里是vb版本(警告-来自c dev!)

    Using file as FileStream = File.OpenText(filename)
        while Not file.EndOfStream
             Dim line as string = file.ReadLine()
         ''//Test to break
         ''//exit while if condition met
        End While
    End Using
    
        3
  •  0
  •   Joel Coehoorn    16 年前

    我通常更喜欢vb.net,但C的迭代器块正在慢慢赢得我的青睐:

    public static IEnumerable<string> SkimFile(string FileName)
    {
        long delta = new FileInfo(FileName).Length / 100;
        long position = 0;
    
        using (StreamReader sr = new StreamReader(FileName))
        {
            while (position < 100)
            {
                sr.BaseStream.Seek(position * delta, SeekOrigin.Begin);
                yield return sr.ReadLine();
                position++;
            }
        }
    }
    

    把它放在类库项目中,然后从vb中使用它,如下所示:

    Dim isMatch as Boolean = False
    For Each s As String in SkimFile("FileName.txt")
        If (RequestedDate - CDate(s.SubString(3,11))).Minutes > 2 Then
           isMatch = True
           Exit For
        End If
    Next s
    

    (我对您的条件(假定固定宽度值而不是分隔值)进行了一些自由处理,以使示例更简单)

        4
  •  0
  •   Dan Diplo    16 年前

    有一个 example on MSDN .

    根据评论进行编辑:

    我必须承认,我有点困惑,因为您似乎坚持使用缓冲文件流,但想一次读取一个文件一行吗?您只需使用一个streamreader就可以做到这一点。我不知道vb,但在c中,它是这样的:

    using (StreamReader sr = File.OpenText(pathToFile)) 
    {
        string line = String.Empty;
        while ((line = sr.ReadLine()) != null) 
        {
            // process line
        }
    }
    

    http://msdn.microsoft.com/en-us/library/system.io.file.aspx .