Files and streams: taking apart a line of text

cfisher440

Well-known member
Joined
Oct 11, 2005
Messages
73
Programming Experience
1-3
I would like to read a text file down to a certain point
ex. Read file until number = 37721.
From there it would have to take some lines below that apart and spew out the info needed, (example, go down 6 lines, take that line apart read in the appropriate data and write it to another text file or database)

EX A B C D E F G
12 34 67 23 1 78 12
In that case go down to the line with the numbers and read in all the numbers.

I have read countlessly on articles describing how to read in files, but they all seem to read in whole files and do not concentrate on individual parts of a file.

Any ideas would be appreciated.
 
You can use the ReadLine function to read a single line of text or the Read function to read one character. For example:
VB.NET:
Dim sr as IO.StreamReader = new IO.StreamReader("C:\filename.txt")
Dim strLine as String = sr.ReadLine() 'Read in one line of text and advance the reader one line
Dim intChar as Integer
While sr.Peek > -1 'Check the next character without advancing the reader
    intChar = sr.Read() 'Read one character and advance the reader one character
    'Do some stuff
End While
 
Thanks for the feedback, but. . .

The idea is to read a file like the one attached to the number 32771 (toward the top right of the file (says sequence # 32771)), skip down 14 lines, read that line and extract the data (People1, 171, 171, 0, 171, 171, etc… ) and write it to a another file or database.
Read down another 15 lines and only read in the numbers under "total" and "xfer".
I am finding this very difficult to do due to there being so much data that I don’t need.

I thank you for your comments as they are very helpful and would appreciate any more ideas or/and more feedback
 
Sorry... forgot to attach file

The attached file of the example.
Best to view using Notepad (or other similar program).
 

Attachments

  • example.txt
    5.6 KB · Views: 33
Here are a couple functions I put together which might help you out. The FindSequenceLine function may or may not be useful to you. It returns the line number of a specific sequence number. You send the full file text as a string and the sequence number as a string and it will tell you what line it is on. I thought you might need that to know where to start counting.

The GetData function will return individual pieces of data to you from a line. Just send it a single line of text from your file and a number corresponding to the column (so to speak) of data you want. For example, if you wanted the data "171" right after "People1", you would call: GetData(lineoftext,1). If you call GetData(lineoftext,0) it will return the first element - in this case "People1".

It is unfortunate that the data in the file is not separated by TABs. That would make things easier and less prone to errors. I wrote the GetData function with the assumption that you will never have a space within any of your data. If, for example, you were to ever have "People 1" instead of "People1" it would throw everything off.

I hope this helps you.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] StreamReader = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamReader("C:\example.txt")
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16 = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 27 [/SIZE][SIZE=2][COLOR=#008000]' This will get us to line 27
[/COLOR][/SIZE][SIZE=2]  sr.ReadLine()
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = sr.ReadLine [/SIZE][SIZE=2][COLOR=#008000]'Capture line 28
[/COLOR][/SIZE][SIZE=2]sr.Close()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] data0, data1, data2, data3, data4 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Variables for our data
[/COLOR][/SIZE][SIZE=2]data0 = GetData(sLine, 0)
   data1 = GetData(sLine, 1)
   data2 = GetData(sLine, 2)
   data3 = GetData(sLine, 3)
   data4 = GetData(sLine, 4)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] FindSequenceLine([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] FullText [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#0000ff] ByVal[/COLOR][/SIZE][SIZE=2] SequenceNum [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = FullText.Split(ControlChars.NewLine)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] r [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Regex = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Regex("SEQUENCE # " & SequenceNum)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] m [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Match
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SeqLine [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32 = -1
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32 = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] s.GetUpperBound(0)
      m = r.Match(s(i))
[/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] m.Success [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]     SeqLine = i
[/SIZE][SIZE=2][COLOR=#0000ff]     Exit [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] SeqLine
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetData([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] LineOfText [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] DataNumber [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] currNumber [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16 = -1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sData [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] blnSpaces [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int16 = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] LineOfText.Length - 1
[/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] LineOfText.Chars(i) = " " [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] LineOfText.Chars(i) = " " [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] LineOfText.Chars(i) <> " " [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]True [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]  currNumber += 1
[/SIZE][SIZE=2][COLOR=#0000ff]ElseIf[/COLOR][/SIZE][SIZE=2] LineOfText.Chars(i) <> " " [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] currNumber = DataNumber [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] blnSpaces = [/SIZE][SIZE=2][COLOR=#0000ff]False [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]  sData &= LineOfText.Chars(i)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] sData
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]

 
If you are going to be doing a lot of work with each file, you may find it better to read the whole thing at once and store it. For example:

VB.NET:
dim sFullText as String = sr.ReadToEnd()
 
dim linenum as Int32 = FindSequenceLine(sFullText, "32771") 'To use the FindSequenceLine function
 
Thank you for the reply

The logic you set forth is very helpful and I appreciate it. With all the files, sequence number 32771 is where it must read up to, but knowing which line 32771 will be on is unknown.

From my understanding of the FindSequenceLine function
dim linenum as Int32 = FindSequenceLine(sFullText, "32771")
Will return the line number that "32771" is on?

Also I will be storing this data. It will be going to a database I have created.
So next step, writing that data to the database accordingly.

I have a book on Visual Basic.Net from Deitel and Deitel, it's a good book, but did not take mention of a lot of the objects you included in your code which is very helpful in file reading.

Once again, thank you for your comments, and I always appreciate any more ideas and/or feedback.
 
Yes, it will return the line number.

If you are going to be doing a lot of this type of work, you will definitely want to look into Regular Expressions, which you will find in the System.Text.RegularExpressions namespace. Regular Expressions allow you to find text within text by matching patterns. It is very powerful. If you are new to programming, it won't be the easiest thing you've learned, but it is very rewarding and, in some cases, almost essential.
 
Back
Top