0

I'm using Visual Basic (Visual Studio 2010) to open and read a large text file that isn't delimited (the file is produced by a UNIX based program). I need to parse that long line into rows of 130 characters. I do know how to read the file. But I don't know how to break this up into those rows. Can you help?

In advance, thanks for your assistance!

Don

Don Wilson
  • 41
  • 2
  • 8
  • What did you read with, and into? Some code would be nice... – Idle_Mind Dec 16 '14 at 17:53
  • @Idle_Mind, I'm using StreamReader to pull the text in. RestranName = "C:\Restran Conversion\IPSDATA\PM00213A\20141210.*" Dim StreamReader As New System.IO.StreamReader(RestranName) – Don Wilson Dec 16 '14 at 18:12

2 Answers2

1

Create StreamReader strReader = New StreamReader("file.txt")

And use the method StreamReader.read(new char[130], 0, 130);

The 130 first character will be placed into the char array. Then, you have to offset until the end of the file.

The documentation of the function is here: http://msdn.microsoft.com/en-us/library/9kstw824(v=vs.110).aspx

Edit:

An even better way will be to use the readBlocks method as suggested here : When to use StreamReader.ReadBlock()?

char buffer = char[130];
int len = 0;
while((len = strReader.ReadBlock(buffer, 0 , 130)) != 0)
{
  Dim value As String = New String(buffer);
  Console.WriteLine(value);
}

The while will go on until there are remaining characters in your file. The value String contains your line of 130 character and you can do whatever you want with it.

Community
  • 1
  • 1
Mathieu Nls
  • 2,285
  • 2
  • 17
  • 32
  • I got an error message with 'StreamReader strReader = New StreamReader("file.txt")'. I've re-written this to be 'Dim StreamReader As New System.IO.StreamReader(RestranName)'. With this said, I'm not sure what you mean by "offset until the end of the file". Can you explain further? – Don Wilson Dec 16 '14 at 18:18
0

Here's a VB version of Mathieu Nls ReadBlock() with some minor changes and fixes:

    Dim charsRead As Integer
    Dim buffer(129) As Char ' 0 to 129 = 130 chars
    Using SR As New System.IO.StreamReader(RestranName)
        While Not SR.EndOfStream
            charsRead = SR.ReadBlock(buffer, 0, buffer.Length)
            If charsRead > 0 Then
                Dim str As New String(buffer, 0, charsRead)
                Debug.Print(str)
            End If
        End While
    End Using
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Brilliant. Simply brilliant! This is exactly what I needed. I've got some more testing to do. It didn't work perfectly, but I don't have the actual file that it is to parse. I should have access to that later. Hopefully that test will prove it. Thank you. I've been searching and searching for something for hours to make this work. This appears to do it. – Don Wilson Dec 17 '14 at 00:51
  • I'm testing with the actual file now. I've been playing around with the length of the rows (currently set at 127). While that looks partially correct, it has a row inserted after the first set of characters, another row after second and the same after the third (these first three row would be the "header" of the report). At the forth row, where the "column names" are listed, there doesn't appear to be 127 characters in that row (I didn't count, but I'm guessing there is half of that) as this row wraps into a fifth row. The same happens from that point down. Any idea what is happening? – Don Wilson Dec 17 '14 at 03:41
  • Sorry...since I didn't create the file, I have no idea how it is formatted and arranged. – Idle_Mind Dec 17 '14 at 04:26
  • lol! I'll keep working to figure out what's going on. It's unlike anything I've seen. I've used the editor in EXCEL to parse it and it looks fine (except it adds quotes to the lines). I'm guessing that there may be some carriage returns in there somewhere. But thanks again for the assistance. – Don Wilson Dec 17 '14 at 13:55