473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading a text file

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
Nov 20 '05 #1
11 1252
Cor
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
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?

Nov 20 '05 #2
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.
"Cor" <no*@non.com> wrote in message
news:er**************@tk2msftngp13.phx.gbl...
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
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?


Nov 20 '05 #3
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
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OT****************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #4
Thanks Philip.

Isn't there a way to send the reader directly to the last line?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OT****************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5
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" <ca**************@midwestern.net> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
Thanks Philip.

Isn't there a way to send the reader directly to the last line?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OT****************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #6
"Catalin Porancea" <ca**************@midwestern.net> schrieb

Isn't there a way to send the reader directly to the last line?


Where does the last line start?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
Thanks Philip. I'll give it a try.
"Philip Rieck" <st***@mckraken.com> wrote in message
news:eD**************@TK2MSFTNGP11.phx.gbl...
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" <ca**************@midwestern.net> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
Thanks Philip.

Isn't there a way to send the reader directly to the last line?

"Philip Rieck" <st***@mckraken.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
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
"Catalin Porancea" <ca**************@midwestern.net> wrote in message
news:OT****************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #8
I don't know. The files vary in size and number of records.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Catalin Porancea" <ca**************@midwestern.net> schrieb

Isn't there a way to send the reader directly to the last line?


Where does the last line start?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
"Catalin Porancea" <ca**************@midwestern.net> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #10
Thanks Armin.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:ur*************@tk2msftngp13.phx.gbl...
"Catalin Porancea" <ca**************@midwestern.net> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11
On Fri, 9 Jan 2004 16:09:21 -0600, Catalin Porancea wrote:
I don't know. The files vary in size and number of records.

Isn't there a way to send the reader directly to the last line?


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
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.