Read text file character by character

skylabnn

Member
Joined
Nov 11, 2009
Messages
10
Programming Experience
1-3
Please help me.
I want to read a text file(.txt) character by character into a database.
There are 28 characters on each line and I want to read the first 16 into a column in a database and the rest in another column in the database.

Please help me (VB.NET code)
Thank you
 
VB.NET:
Dim field1(15) As Char
Dim field2 As String

Using reader As New StreamReader("file path here")
    Do Until reader.EndOfStream
        reader.Read(field1, 0, field1.Length)
        field2 = reader.ReadLine()

        MessageBox.Show(field2, New String(field1))
    Loop
End Using
The message box is just an example. You can do whatever you want with the fields, e.g. insert them into a database.
 
Thank you so very much.
Please after reading the first 16 characters, I want to read the next 12 characters.
Thank you in advance
 
If you take a look at the "TextFieldParser" in the help index it has an example that shows how to read fixed-width files. You just have to set the width of each of your two columns.

VB.NET:
Using Reader As New TextFieldParser("C:\TestFolder\test.log")

   Reader.TextFieldType = FieldType.FixedWidth
   Reader.SetFieldWidths(16, 12)

   Dim currentRow As String()

   While Not Reader.EndOfData

         currentRow = Reader.ReadFields()
         
         Messagebox.Show("Field 1 : " & currentRow(0))
         Messagebox.Show("Field 2 : " & currentRow(1))

   End While

End Using
 
This seems to be the same thread as Printing Data Read from a Text File, please limit it to one thread. I will post any further comments in the other thread rather then replying to both.
The original topic was about reading the data so, when the question of printing the data was asked I separated it into another thread. Reading files and printing data are two different topics.
 
Please how can I limit the number of rows to import?
Say from 500 rows of Data I want to import only 100
I also want to delete those rows imported so that they will not be imported again.
 
You'll need to overwrite the file with the records that you don't use. Here's an example you're reading the first 5 lines of the file and doing something with them. The remaining lines are read into a string and overwrite your existing file.

VB.NET:
		Dim contents As String
		Using sr As New IO.StreamReader("C:\Temp\Records.txt")
			For i As Integer = 0 To 4
				sr.ReadLine()
				'Process your line
			Next
			contents = sr.ReadToEnd()
		End Using

		Using sw As New IO.StreamWriter("C:\Temp\Records.txt", False)
			sw.Write(contents)
		End Using
 
Thanks.
Please how can I start reading from a particular row?
I want to start reading from the 12th row of the text file.
You can't. You have to start reading from the beginning. If you only want the data from the 12th line then simply discard the first 11 lines.
 
Thanks.
How do I do that?
How can I discard the first 11 lines?
As I am reading the data I am also writing it to a database.
Thanks
 
Thanks.
How do I do that?
How can I discard the first 11 lines?
As I am reading the data I am also writing it to a database.
Thanks
That's like asking how to not eat the first 6 doughnuts in a dozen. If you don't want to write the first 11 lines then don't write them. Count the lines as you read and don't write until you've counted 12.
 
That's like asking how to not eat the first 6 doughnuts in a dozen. If you don't want to write the first 11 lines then don't write them. Count the lines as you read and don't write until you've counted 12.

Why would you not eat the first 6 doughnuts in a dozen? Wouldn't you want to just eat them all? :p
 
Back
Top