Parsing text into rows of 130 characters in VB.net - vb.net

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

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.

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

Related

Is it possible to use String.Split() when NewLine is the delimiter?

I have a question which asks me to calculate something from an input file. The problem is, the lines in the file don't use any special character as delimiter, like , or |. I will show it down below.
Data Communication
20
Visual Basic
40
The output I need to write to another file should look like this:
Data communication 20
Visual Basic 40
Total Books : 60
The problem is, how can I specify the delimiter? Like when there is a symbol as in strArray = strLine.Split(","). Since there is nothing I can use as delimiter, how can I split the file content?
There's no real need to split the text in the input file, when you can read a file line by line using standard methods.
You can use, e.g., a StreamReader to read the lines from the source file, check whether the current line is just text or it can be converted to a number, using Integer.TryParse and excluding empty lines.
Here, when the line read is not numeric, it's added as a Key in a Dictionary(Of String, Integer), unless it already exists (to handle duplicate categories in the source file).
If the line represents a number, it's added to the Value corresponding to the category Key previously read, stored in a variable named previousLine.
This setup can handle initial empty lines, empty lines in the text body and duplicate categories, e.g.,
Data Communication
20
Visual Basic
40
C#
100
Visual Basic
10
Other stuff
2
C++
10000
Other stuff
1
If a number is instead found in the first line, it's treated as a category.
Add any other check to handle a different structure of the input file.
Imports System.IO
Imports System.Linq
Dim basePath = "[Path where the input file is stored]"
Dim booksDict = New Dictionary(Of String, Integer)
Dim currentValue As Integer = 0
Dim previousLine As String = String.Empty
Using sr As New StreamReader(Path.Combine(basePath, "Books.txt"))
While sr.Peek > -1
Dim line = sr.ReadLine().Trim()
If Not String.IsNullOrEmpty(line) Then
If Integer.TryParse(line, currentValue) AndAlso (Not String.IsNullOrEmpty(previousLine)) Then
booksDict(previousLine) += currentValue
Else
If Not booksDict.ContainsKey(line) Then
booksDict.Add(line, 0)
End If
End If
End If
previousLine = line
End While
End Using
Now, you have a Dictionary where the Keys represent categories and the related Value is the sum of all books in that category.
You can Select() each KeyValuePair of the Dictionary and transform it into a string that represents the Key and its Value (Category:Number).
Here, also OrderBy() is used, to order the categories alphabetically, in ascending order; it may be useful.
File.WriteAllLines is then called to store the strings generated.
In the end, a new string is appended to the file, using File.AppendAllText, to write the sum of all books in all categories. The Sum() method sums all the Values in the Dictionary.
Dim newFilePath = Path.Combine(basePath, "BooksNew.txt")
File.WriteAllLines(newFilePath, booksDict.
Select(Function(kvp) $"{kvp.Key}:{kvp.Value}").OrderBy(Function(s) s))
File.AppendAllText(newFilePath, vbCrLf & "Total Books: " & booksDict.Sum(Function(kvp) kvp.Value).ToString())
The output is:
C#:100
C++:10000
Data Communication:20
Other stuff:3
Visual Basic:50
Total Books: 10173
Sure.. System.IO.File.ReadAllLines() will read the whole file and split into an array based on newlines, so you'll get an array of 4 elements. You can process it with a flipflop boolean to get alternate lines, or you can try and parse the line to a number and if it works, then its a number and if not, it's a string. If it's a number take the string you remembered (using a variable) from the previous loop
Dim arr = File.ReadALlLines(...)
Dim isStr = True
Dim prevString = ""
For Each s as String in arr
If isStr Then
prevString = s
Else
Console.WriteLine($"The string is {prevString} and the number is {s}")
End If
'flip the boolean
isStr = Not isStr
Next s
I used File.ReadAllLines to get an array containing each line in the file. Since the size of the file could be larger than the sample shown, I am using a StringBuilder. This save having to throw away and create a new string on each iteration of the loop.
I am using interpolated strings indicated by the $ preceding the quotes. This allows you to insert variables into the string surrounded by braces.
Note the Step 2 in the For loop. i will increment by 2 instead of the default 1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines = File.ReadAllLines("input.txt")
Dim sb As New StringBuilder
Dim total As Integer
For i = 0 To lines.Length - 2 Step 2
sb.AppendLine($"{lines(i)} {lines(i + 1)}")
total += CInt(lines(i + 1))
Next
sb.AppendLine($"Total Books: {total}")
TextBox1.Text = sb.ToString
End Sub

Reading a filestream issue

I am trying to read a file from my computer and output its contents into a literal control. It gives the error : Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. This is my first time using FileStream and I'm not 100% about all of the syntax for VB, or if there's even a community-preferred way of reading from a file, but could somebody help me out with this error?
This is the code:
Using fs As New FileStream(_path, FileMode.Open, FileAccess.Read)
Try
Dim fileLength As Integer = CInt(fs.Length)
Dim buffer() As Byte = New Byte() {fileLength}
Dim count As Integer
Dim sum As Integer = 0
While ((count = fs.Read(buffer, sum, fileLength - sum)) > 0)
sum = sum + count
End While
litOutput.Text = buffer.ToString()
Catch ex As Exception
'TODO: log error
End Try
End Using
This line is wrong
Dim buffer() As Byte = New Byte() {fileLength}
It declares an array of 1 byte in which you try to store the length of your file. Probably you have Option Strict set to Off and thus you could go away without noticing immediately the problem.
Of course, if your file is of a reasonable length and it is a simple textfile then there is no need for this loop. Just use File.ReadAllText or File.ReadLines or File.ReadAllLines, and by the way, your code read all your data in a single call because the last parameter of FileStream.Read is the quantity of bytes to read from file and the expression fileLength - sum produces a request to read all the bytes in a single call
Instead if you want to read your file in chunks of certain sizes then
probably you need
Using fs As New FileStream(path, FileMode.Open, FileAccess.Read)
Try
Dim chunkSize = 2000
Dim fileLength As Integer = CInt(fs.Length)
Dim buffer(fileLength) as Byte
Dim blockSize(chunkSize) as Byte
Dim count As Integer = -1
Dim pos As Integer = 0
While count <> 0
count = fs.Read(blockSize, 0, chunkSize-1)
Array.Copy(blockSize, 0, buffer, pos, count)
pos += count
End While
litOutput.Text = Encoding.UTF8.GetString(buffer)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
Notice that this code assumes that your file is a Text file with UTF8 encoding.
It this is not the case then you could try with Encoding.ASCII.GetString or Encoding.Unicode.GetString

How to replace bytes in VB.NET?

I have two strings:
Dim Original_Hex_Bytes as string = "616572646E61"
Dim Patched_Hex_Bytes as string = "616E64726561"
Then I have a binary file and I need to search for the Original_Hex_Bytes and replace them with Patched_Hex_Bytes; I don't konw the offset where begin to write new bytes :(
How can I do this?
If needed, I also know how to convert Hex strings in bytes, I use this:
Private Function Hex_To_Bytes(ByVal strinput As String) As Byte()
Dim i As Integer = 0
Dim x As Integer = 0
Dim bytes(strinput.Length / 2) As Byte
Do While (strinput.Length > i + 1)
Dim lngDecimal As Long = Convert.ToInt32(strinput.Substring(i, 2), 16)
bytes(x) = Convert.ToByte(lngDecimal)
i += 2
x += 1
Loop
Return bytes
End Function
You can use BinaryReader and BinaryWriter classes to achieve this.
But in this case, as you do not know the file structure, need to read the entire file and sweep it in search of bytes array and will be easier to use ASCII strings as aerdna and andrea.
When you know the structure of a file is more appropriate to work with data structure to manipulate its contents.

Checking is a specific byte in a file is set to a specific value in vb

I want to be able to make is so that when my program loads it checks to see if the bytes in the file are set to something specific. If they are set to a certain byte then carry out the code.
So I want to be able to make it go to a specific byte, check if it's a certain byte and if it is that certain byte then carry out the code else if it is something else then carry out the other code.
I tried this:
Dim bytes As Byte() = New Byte(writeStream.Length) {}
Dim ByteResult As Integer = writeStream.Read(bytes, 30, 1)
MsgBox(ByteResult)
But it didn't work because for some reason it always returned 1.
I also tried this:
Dim dataArray(60) As Byte
If dataArray(30) <> writeStream.ReadByte() - 0 Then
MsgBox("The bytes have been checked.")
End If
But that didn't seem to work either because it never opened the message box for me.
For example, I want it to seek to offset 30 and then check if the byte is 00 then I want it to carry out code 1 and else if it is 01 I want it to carry out code2.
Thanks.
I found that the following code works:
fLocation = ("file.txt")
Dim writeStream As New FileStream(fLocation, FileMode.Open)
Dim writeBinary As New BinaryWriter(writeStream)
writeStream.Seek(30, SeekOrigin.Begin)
Dim ByteResult = writeStream.ReadByte()
MsgBox(ByteResult)
You can also do this
Dim FileStream1 As New IO.FileStream("File.txt", IO.FileMode.Open)
FileStream1.Position = 30
MsgBox(FileStream1.ReadByte)
FileStream1.Close()
FileStream1.Dispose()

Reading a part of a txt file in VB.NET

I need to read a txt file part by part...
For example, in a txt file: (age,5char_name)
17susan23wilma25fredy
I need to read firstly 17susan. In other words, the first seven characters and after 23wilma and 25fredy, but I'm not reading the whole file and substring the file record. Is there a way to do this via streamreader?
Records are always seven bytes... 2 bytes for age, 5 bytes for name and all records in a line. There is no jump into the next line.
I think there is the solution:
Dim filestream As New FileStream("\records.txt", FileMode.Open)
Dim streamreader As New StreamReader(fs)
Dim buffer(7) As Char
bw.ReadBlock(buffer, 0, 7)
Console.WriteLine(buffer)
this is read first 7.. you can read other via loop or for..
If you ALWAYS want to use the first 7 characters, you can simply use Visual Basic line of code like:
Dim Result as String = Microsoft.VisualBasic.Left(string, no)
String is the StreamReader line from which you want to read only first seven characters.
No is any integer value and it is equal to the number of characters you want to read, in this case 7.
Also, in the same way, you can use Right and Mid to select a string from the right or somewhere in the middle.
Assuming a static number of characters per record (seven in your description), you could use a FileStream reader instead, and use the Read method to retrieve seven chars at a time.
For example:
Const chunkSize As Integer = 7
Using inputFile = File.OpenRead("namefile.txt")
Dim bytesRead As Integer
Dim buffer = New Byte(chunkSize - 1) {}
bytesRead = inputFile.Read(buffer, 0, buffer.Length)
while bytesRead = 7
'Process the buffer here
bytesRead = inputFile.Read(buffer, 0, buffer.Length)
End While
End Using
(code isn't tested, but should be close)