Reading a text file

  • Thread starter Thread starter Catalin Porancea
  • Start date Start date
C

Catalin Porancea

Hello,

I have a txt file that I need to validate before importing in a database. In
order to be valid, the file needs to have a header, records and a trailer.
How do I read only the first and the last line in the file and load them in
2 strings so I can compare them?

Thank you.

Catalin
 
Hi Catalin,

I think the most efficient method is to read all lines in an arraylist and
then to compare the last item from that arraylist with the first.
If Ok you can fill the database from the arraylist.

(This of course for relative not to big textfiles)

Just my thought about this.

Cor
 
Thanks Cor.

That might be an option. How do I do it?
However, it would be great to have a faster method, because the files can
have several hundred thousends records.
 
This console app code may help, it does what you ask. Create a new Console
application, and paste this in to Module1.vb, replacing whats already there.

Imports System.IO

Module Module1

Sub Main()
Dim firstLine As String = Nothing
Dim lastLine As String = Nothing
Dim tmpLine As String = String.Empty

Dim sr As StreamReader = New StreamReader("c:\test.txt")
firstLine = sr.ReadLine()
While Not tmpLine Is Nothing
lastLine = tmpLine
tmpLine = sr.ReadLine()
End While
sr.Close()

If String.Compare(lastLine, firstLine, True) = 0 Then

Console.WriteLine("They Match")

Else

Console.WriteLine("They DO NOT Match")
End If
End Sub

End Module
 
Thanks Philip.

Isn't there a way to send the reader directly to the last line?
 
This may work for you: it's sparse for clarity of concept, and assumes that
there are NO extra characters after the last line (no spaces, newlines,
nothing!). You really should add error handling to this, as it assumes too
many things - I've left it out again for concept. (It assumes the file
exists, that it has more than one line, etc)

Imports System.IO

Module Module1

Sub Main()
Dim firstLine As String = Nothing
Dim lastLine As String = Nothing

Dim sb As New System.Text.StringBuilder
Dim readCount As Integer
Dim readByte As Integer

Dim br As New System.IO.FileStream("c:\test.txt", FileMode.Open)
readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readCount += 1
readByte = br.ReadByte()
End While

firstLine = sb.ToString()
sb.Length = 0
br.Seek(-1 * readCount, SeekOrigin.End)

readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readByte = br.ReadByte()
End While
lastLine = sb.ToString()

If String.Compare(lastLine, firstLine, True) = 0 Then

Console.WriteLine("They Match")

Else

Console.WriteLine("They DO NOT Match")
End If
End Sub

End Module
 
Thanks Philip. I'll give it a try.


Philip Rieck said:
This may work for you: it's sparse for clarity of concept, and assumes that
there are NO extra characters after the last line (no spaces, newlines,
nothing!). You really should add error handling to this, as it assumes too
many things - I've left it out again for concept. (It assumes the file
exists, that it has more than one line, etc)

Imports System.IO

Module Module1

Sub Main()
Dim firstLine As String = Nothing
Dim lastLine As String = Nothing

Dim sb As New System.Text.StringBuilder
Dim readCount As Integer
Dim readByte As Integer

Dim br As New System.IO.FileStream("c:\test.txt", FileMode.Open)
readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readCount += 1
readByte = br.ReadByte()
End While

firstLine = sb.ToString()
sb.Length = 0
br.Seek(-1 * readCount, SeekOrigin.End)

readByte = br.ReadByte()
While readByte <> -1 AndAlso readByte <> 13
sb.Append(Convert.ToChar(readByte))
readByte = br.ReadByte()
End While
lastLine = sb.ToString()

If String.Compare(lastLine, firstLine, True) = 0 Then

Console.WriteLine("They Match")

Else

Console.WriteLine("They DO NOT Match")
End If
End Sub

End Module
 
Catalin Porancea said:
I don't know. The files vary in size and number of records.

Right. That's why you can't send the reader directly to the last line.
 
I don't know. The files vary in size and number of records.

Are the header and footer records the same length? Do they vary in length?
Are the individual records a fixed length?

If so, you can calculate the starting position of the last line and seek to
that point in the stream. If the records are not fixed length, then you
won't be able to use this approach.

Chris
 
Back
Top