Reading and Parsing a .txt file (Tab seperated variables)

Craig

Well-known member
Joined
Oct 15, 2005
Messages
110
Location
United Kingdom
Programming Experience
Beginner
Hi, I have a text file that I need to read from here is a small section of it:

id TitleValue First Name Last Name Organisation Address City PostCode Country Work Phone Mobile Phone Fax Number Email Website Team Name No. Competitors No. Spectators Wall Follower Maze Solver Additional Info Competition Enquiry Type Submitted
7 Miss Verona Needham Technology Innovation Centre Contract Compliance & Project AdministrationTechnology Innovation CentreMillennium PointCurzon Street Birmingham B4 7XG United Kingdom 0121 331 7417 0121 331 5401 <e-mail address removed> tic-online.co.uk 0 3 0 0 2004 0 03/02/2004 15:59:01
8 Dr Anthony Wilcox TIC Millennium PointCurzon Street Birmingham B4 7XG United Kingdom 01543 877158 07929 207051 0121 331 5401 <e-mail address removed> micromouse.tic.ac.uk Significance of the PICkle 1 1 1 1 No previous history ... new to competition 2004 0 03/03/2004 09:30:45


Basically, I want to use a multi-dimensional array which will be populated on the form's load event. For example VarArray(0,0) will be 'id' etc

The main issue is being able to seperate each piece of info when it is read from the txt file..

Thanks!
 
Last edited by a moderator:
when the error occurs the Id length = 1 and arraypos = 1

When I changed the code to get the file location to this:

VB.NET:
    Private Sub Btn_Locate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Locate.Click
        FileDialog.Filter = "Text Files (*.txt)|*.txt"
        FileDialog.ShowDialog()
        FileLocation = FileDialog.FileName
        Lbl_FileLocation.Text = FileLocation
        SR = New StreamReader(FileLocation)
        AddRecords()
    End Sub

It now breaks on:

VB.NET:
   Private Sub SetArrayVals()
        a = 0
        Dim StrText As String
        Do While SR.Peek <> -1
            StrText = SR.ReadLine
            Dim Splitted() As String = StrText.Split(vbTab)

            [COLOR=Red]Id(a) = Splitted(0).Trim '''Where ID length = 1 and a = 1[/COLOR]
            TitleValue(a) = Splitted(1).Trim
            FirstName(a) = Splitted(2).Trim
            LastName(a) = Splitted(3).Trim
            Organisation(a) = Splitted(4).Trim
            Address(a) = Splitted(5).Trim
            City(a) = Splitted(6).Trim
            PostCode(a) = Splitted(7).Trim
            Country(a) = Splitted(8).Trim
            WorkPhone(a) = Splitted(9).Trim
            MobilePhone(a) = Splitted(10).Trim
            FaxNumber(a) = Splitted(11).Trim
            Email(a) = Splitted(12).Trim
            Website(a) = Splitted(13).Trim
            TeamName(a) = Splitted(14).Trim
            NumCompetitors(a) = Splitted(15).Trim
            NumSpectators(a) = Splitted(16).Trim
            WallFollower(a) = Splitted(17).Trim
            MazeSolver(a) = Splitted(18).Trim
            AdditionalInfo(a) = Splitted(19).Trim
            Competition(a) = Splitted(20).Trim
            EnquiryType(a) = Splitted(21).Trim
            Submitted(a) = Splitted(22).Trim
            a += 1
        Loop
    End Sub
Thanks for all the help so far!!
 
Craig said:
when the error occurs the Id length = 1 and arraypos = 1
You didn't know arrays are zero-based? Id length 1 means there is one element in array and you access it like this: Id(0)
 
JohnH said:
You didn't know arrays are zero-based? Id length 1 means there is one element in array and you access it like this: Id(0)
Yeah i knew that but, how come the first time i run the code eg. on load event it works correctly but when I call the procedures again (on locating a new text file) it doesnt work!


Also how would I be able to find out if there file that i preset cant be found

Basically, in pseudo-code:
if ..\micromousecompetitionsample.text cant be found then
filedialog.showdialog
filelocation = filedialog.filename
end if
 
Last edited:
You can set the filedialog property CheckFileExists to True and...
VB.NET:
if FileDialog.ShowDialog() = dialogresult.OK then
        FileLocation = FileDialog.FileName
        Lbl_FileLocation.Text = FileLocation
        SR = New StreamReader(FileLocation)
        AddRecords()
end if
 
Thats not quite what i want it to do, basically if the file " F:\micromousecompetitionsample.txt" cant be found open the file dialog to locate a file to be used
Maybe I'm just being slow:confused:
 
Last edited:
The Exists method of File class in System.IO namespace is nice.
 
haha im loving the hints there

this is what ive put

VB.NET:
        If Not System.IO.File.Exists("F:\MicromouseCompetitorsSample.txt") Then
            FileDialog.Filter = "Text Files (*.txt)|*.txt"
            FileDialog.ShowDialog()
            FileLocation = FileDialog.FileName
            Lbl_FileLocation.Text = FileLocation
        End If
well that doesnt work but ill try some other stuff

nvm got it to work with adding these cheeky lines in

SR = New StreamReader(FileLocation)
SR_LineCount = New StreamReader(FileLocation)
 
back again and this time its to do with streamwriter :|

I want the code to re-write the data from the arrays into the text file ive tried to do this by:

VB.NET:
    Private Sub Frm_Main_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        SR.Close()
        SR_LineCount.Close()

        Dim SW As StreamWriter = New StreamWriter(FileLocation)
        Dim StrToWrite As String
        Dim IntCounter As Integer

        For IntCounter = 0 To Id.Length - 1
            StrToWrite = Id(IntCounter) + vbTab + [COLOR=Blue]shortened[/COLOR] + vbTab + Submitted(IntCounter)
            SW.WriteLine(StrToWrite)

        Next
    End Sub
However this doesnt write the last 3 records into the text file!!
 
I'm still getting this problem but now its just cutting off the end of the last record instead of the last 3 records (progress i think) eg: it should save this in the txt file:
id TitleValue First Name Last Name Organisation Address City PostCode Country Work Phone Mobile Phone Fax Number Email Website Team Name No. Competitors No. Spectators Wall Follower Maze Solver Additional Info Competition Enquiry Type Submitted
...number of lines here
1010 Dr Arat Al-baiman Sharif University of Technology Center of excellence in Robotics Automation and Technology Tehran TT99 9FN Iran email CERAT 4 4 0 1 Our team has the best record of maze solving in Iran. 2006 2 19/03/2006 06:47:38

but instead I get:


id TitleValue First Name Last Name Organisation Address City PostCode Country Work Phone Mobile Phone Fax Number Email Website Team Name No. Competitors No. Spectators Wall Follower Maze Solver Additional Info Competition Enquiry Type Submitted
...number of lines here
1010 Dr Arat Al-baiman Sharif University of Technology Center of excellence in Robotics Automation and Technology Tehran TT99 9FN Iran email CERAT 4 4 0 1 Our team has the best record of maze '''rest missing
 
I did an example you should have a go at, zip file of project is attached to this post.
It's back to my first suggestion using a structure and array of records.
It should not be difficult to understand the code, but I was lazy naming the controls.
 

Attachments

  • TabFile.zip
    8.9 KB · Views: 18
Thanks JohnH, thats much simpler than the way i was doing things

Now I'm having trouble with 2 things with this code
1. adding a new, blank record to the end of the list
2. deleting the current record thats on display


For #1 i tried this:

VB.NET:
Private Sub Btn_Add_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Btn_Add.Click
        Dim r As Record
        Records.Add(r)
        r.Id = ""
        [COLOR=Blue]etc[/COLOR]
        r.Submitted = ""

        FillFields(Records.Count)
    End Sub
But to no avail

As for #2.. :confused::confused::confused:

Thanks again



EDIT: Fixed!!
 
Last edited:
Back
Top