Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,075 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,790 people online right now. Registration is fast and FREE... Join Now!




Sequential File

 
Reply to this topicStart new topic

Sequential File

AllAboutMe
4 May, 2008 - 10:01 AM
Post #1

New D.I.C Head
*

Joined: 2 May, 2008
Posts: 1

HELP

My program is to read sequential file into a listbox. A Comma seperate each field in the record . There are 6 fields.
My problem is when the file is read, the records in the file are duplicated and part of the record appears on a different line when placed into the list box. Example of a record in the txt file: 1212,Albertson,William,R,10,04
When read to list box it should only put the 1st thru 4th fields.

CODE

                Public Class galaxyTrekkers

    ' declare members structure
    Structure Members
        Public id As String
        Public lName As String
        Public fName As String
        Public mInt As String
        Public grade As String
        Public classPeriod As String
    End Structure

    ' declare array
    Private member(250) As Members

    Private Sub galaxyTrekkers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      
      ' read file and determine if file exist

        Dim text As String
        Dim newLine As Integer
        Dim recordIndex As Integer
        Dim record As String
        Dim commaIndex As Integer


        If My.Computer.FileSystem.FileExists("StudentList.txt") Then
            text = My.Computer.FileSystem.ReadAllText("StudentList.txt")


            For subscript As Integer = 0 To 7

                ' locate new line
                newLine = _
                     text.IndexOf(ControlChars.NewLine, recordIndex)
                'assign a line
                record = _
                    text.Substring(recordIndex, newLine - recordIndex)



                ' locate comma
                commaIndex = text.IndexOf(",", 0)
                ' assign member to the array
                member(subscript).id = record.Substring(0, commaIndex)
                member(subscript).lName = record.Substring(commaIndex + 1)
                member(subscript).fName = record.Substring(commaIndex + 1)
                member(subscript).mInt = record.Substring(commaIndex + 1)


                ' add the member to the list box
                ListBox1.Items.Add(member(subscript).id)
                ListBox1.Items.Add(member(subscript).lName)
                ListBox1.Items.Add(member(subscript).fName)
                ListBox1.Items.Add(member(subscript).mInt)

                ' update the record index by 6
                recordIndex = newLine + 6
            Next

        Else
            MessageBox.Show("File does not exist", "List", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub              
                              


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Sequential File
5 May, 2008 - 07:30 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Have a look at the String.Split(char()) Method. This method id specifically designed for working with delimited strings, such as in your file.

Also, this is VB.Net code so I'm moving this to that forum smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month