?
Solved

I/O Reading data by chunks

Posted on 2007-11-28
6
Medium Priority
?
158 Views
Last Modified: 2013-11-26
I'm using VS 2005, .net 2.0, and coding in VB.net.
I want to read in a text file that is 26MB in size.  Each record lengh is139 characters long.  Does someone know of a method to loop thru my file reading 139 characters at a time until I get to the end
of the file?

I explored using the StreamReader method but I'm getting the first 10K characters when I use
the ReadLine method.

Please let me know if you need additional information.

Thanks in advance.
0
Comment
Question by:GLlano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Accepted Solution

by:
-DJL- earned 1000 total points
ID: 20370378
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 1000 total points
ID: 20370405
Having a StreamReader sr:

String str as String
Dim buffer(139) as Char

sr.Read(c, 0, c.Length)
str = new String(buffer)
0
 
LVL 3

Expert Comment

by:wizrr
ID: 20370446
You need to read strings? Not binary data? If so you must know how file is encoded and use StreamReader to decode it. After that use Read instead of ReadLine.
Using sr As StreamReader = New StreamReader("TestFile.txt", Encoding.UTF8)
                Dim line As String
                ' Read and display the lines from the file until the end 
                ' of the file is reached.
                Dim buffer As Char[139];
                Dim str As StringBuilder
                Dim read As Int
                Do
                    
                    read = sr.Read(buffer, 0, 139);
                    str.Append(buffer)
                    Console.WriteLine(buffer)
                Loop Until Not read = 0
                sr.Close()
            End Using

Open in new window

0
 
LVL 16

Expert Comment

by:gnoon
ID: 20370522
Simply use TextFieldParser class of .NET 2.0.
See example here http://msdn2.microsoft.com/en-us/library/zezabash(VS.80).aspx
Here is code snippet & modified
Using Reader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\TestFolder\test.log")
   Reader.TextFieldType = _
   Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
   Reader.SetFieldWidths(139)
   Dim currentRow As String()
   While Not Reader.EndOfData
      Try
         currentRow = Reader.ReadFields()
         Dim currentField As String = currentRow(0)
         Console.WriteLine(currentField)
      Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
         MsgBox("Line " & ex.Message & _
         "is not valid and will be skipped.")
      End Try
   End While
End Using

Open in new window

0
 
LVL 1

Expert Comment

by:Computer101
ID: 20615326
Forced accept.

Computer101
EE Admin
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question