tutorial about using DAT files

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 19973 Views - Last Post: 06 September 2011 - 05:46 PM Rate Topic: -----

#1 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

tutorial about using DAT files

Posted 01 September 2011 - 10:23 AM

Hi all i was wondering does any know of any good tutorial on using dat files eg reading writing and formatting.

I am pretty sure you and use stream reader and writer form the IO but what about formatting. Any help would be great

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: tutorial about using DAT files

#2 demausdauth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 190
  • View blog
  • Posts: 693
  • Joined: 03-February 10

Re: tutorial about using DAT files

Posted 01 September 2011 - 11:47 AM

If you need to have the data in a particular format when you save it -- then String.Format would be beneficial. For instance let's say that you need to have a file that has 3 fields in it(First Name, Middle Name, Last Name) and the fields are 25 characters, 25 characters, and 35 spaces. So to print a person's name to the dat file you could:

   
    Private Function GetDataLine(ByVal firstName As String, ByVal middleName As String, ByVal lastName As String) As String

        Dim lineToPrint As String = String.Empty
        Try
            lineToPrint = String.Format("{0,-25}{1,-25}{2,-35}", firstName, middleName, lastName)

        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        Return lineToPrint
    End Function



The negative values in the string format left align the text within the given character length. If you use positive values then the text will be right aligned.
Was This Post Helpful? 2
  • +
  • -

#3 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 01 September 2011 - 01:01 PM

That was very helpful but what if you have a long string all on one line like below:

128/AH675674/524365/th546372/John/White/7464554

And say from this one line i wanted to get the following values

AH675674
th546372
John
White

and just leave the rest of the line out. also the name size may change in size.
Was This Post Helpful? 0
  • +
  • -

#4 demausdauth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 190
  • View blog
  • Posts: 693
  • Joined: 03-February 10

Re: tutorial about using DAT files

Posted 01 September 2011 - 02:26 PM

Ok -- if the line of data that you are pulling in is in the format that you describe, then what you need to do is use the .Split() method of the string. That will give you a 0-based array of strings that you can use to get the information from.
Was This Post Helpful? 0
  • +
  • -

#5 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 01 September 2011 - 02:34 PM

Do mean something like this then:

	
	Dim s As String = "128/AH675674/524365/th546372/John/White/7464554"

	' Split the string on the backslash character
	Dim parts As String() = s.Split(New Char() {"/"c})

	' Loop through result strings with For Each
	Dim part As String
	For Each part In parts
	    Console.WriteLine(part)
	Next

This post has been edited by m_wylie85: 01 September 2011 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 demausdauth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 190
  • View blog
  • Posts: 693
  • Joined: 03-February 10

Re: tutorial about using DAT files

Posted 01 September 2011 - 03:41 PM

Basically yes -- however if the data file isn't going to change in it's formatting (as it shouldn't), then every time the first name should be the 4th index and the last name should be the 5th index. If you know that then you can just directly access them.

    Private Function ParseLine(ByVal stringValue As String) As String()

        Dim returnValues() As String = Nothing
        Try
            returnValues = stringValue.Split("/")

            'the array is a 0-based
            ' 0      1       2      3       4     5    6
            '128/AH675674/524365/th546372/John/White/7464554

            Dim specialValue As String = returnValues(1)
            Dim firstName As String = returnValues(4)
            Dim lastName As String = returnValues(5)


        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try

        Return returnValues
    End Function



Was This Post Helpful? 1
  • +
  • -

#7 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 02 September 2011 - 12:11 AM

nice one that helps a lot thanks bye :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 04 September 2011 - 01:11 PM

Hi all have a problem i can't get my head around this can you please help point me in the right direction.

I am using a dialog box to get a file i want (a DAT file) then i am using stream reader to read all of the text inside the file. then i am trying to split each line so that i can get only the words that i want from each line

mock text would look like this:

'128/AH675671/524365/th546372/John/White/7464554
'129/AH675672/524365/th546372/Jack/White/7464554
'130/AH675673/524365/th546372/Steven/White/7464554
'131/AH675674/524365/th546372/Michael/White/7464554
'132/AH675675/524365/th546372/Mark/White/7464554

but my problem is it only returns the first line with the values i want. i cant figure out how to get the code to do this for each line.

my code: my problem is at line 34/35 i think

    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click

        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        Dim strContents As String

        Dim objReader As StreamReader
        Dim returnValues() As String = Nothing
        Dim go = ""

        openFileDialog1.InitialDirectory = "C:\Users\michael\Desktop"
        openFileDialog1.Filter = "DAT files (*.DAT)|*.DAT|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        ' mock up of the data i am looping through
        '128/AH675671/524365/th546372/John/White/7464554
        '129/AH675672/524365/th546372/Jack/White/7464554
        '130/AH675673/524365/th546372/Steven/White/7464554
        '131/AH675674/524365/th546372/Michael/White/7464554
        '132/AH675675/524365/th546372/Mark/White/7464554

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then

                    objReader = New StreamReader(myStream)
                    strContents = objReader.ReadToEnd ' read all text in file.

                    If strContents IsNot Nothing Then ' check to see if there is text in the file.

                        returnValues = strContents.Split("/")
                        go = returnValues(1) & "   " & returnValues(4) & "   " & returnValues(5)
                    End If
                    RTBUnformatedData.Text = go.ToString
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file " & Ex.Message)
            Finally
                ' Check this again, to make sure i didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

This post has been edited by m_wylie85: 04 September 2011 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: tutorial about using DAT files

Posted 04 September 2011 - 03:06 PM

This is the format you need. Read each line and add to your collection of lines.

Dim lines As New List(Of String)
objReader = New StreamReader(myStream)
While Not objreader.EndOfStream
   Dim str() As String = objReader.Readline.Split("/"c)
   lines.Add(String.Format("{0} {1} {2}", str(1), str(4),str(5))
End While
objReader.Close()
objReader.Dispose() 



Now your list has a formatted string of your data.
Was This Post Helpful? 1
  • +
  • -

#10 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 04 September 2011 - 03:36 PM

Thanks that was driving me mad
Was This Post Helpful? 0
  • +
  • -

#11 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 04 September 2011 - 04:17 PM

Hey i just had a look at that but i still have the same problem it only returns the last line text in the DAT file
Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: tutorial about using DAT files

Posted 04 September 2011 - 05:12 PM

You will have to show the code you used. That should work without problem. Unless you have only one line in file.
Was This Post Helpful? 0
  • +
  • -

#13 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 05 September 2011 - 10:27 AM

Hi hawkvalley thanks for the help i try to work out your code on my own but i am getting nowhere. this is my code:

    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click

        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()



        Dim objReader As StreamReader


        openFileDialog1.InitialDirectory = "C:\Users\michael\Desktop"
        openFileDialog1.Filter = "DAT files (*.DAT)|*.DAT|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True

        ' mock up of the data i am looping through
        '128/AH675671/524365/th546372/John/White/7464554
        '129/AH675672/524365/th546372/Jack/White/7464554
        '130/AH675673/524365/th546372/Steven/White/7464554
        '131/AH675674/524365/th546372/Michael/White/7464554
        '132/AH675675/524365/th546372/Mark/White/7464554

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                Dim lines As New List(Of String)
                objReader = New StreamReader(myStream)
                While Not objReader.EndOfStream
                    Dim str() As String = objReader.ReadLine.Split("/"c)

                    lines.Add(String.Format("{0} {1} {2}", str(1), str(4), str(5)))

                    RTBUnformatedData.Text = str.ToString
                End While

            Catch Ex As Exception
                MessageBox.Show("Cannot read file " & Ex.Message)
            Finally
                ' Check this again, to make sure i didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub


This is how i tried to pass the values in the list to the rich text box but i am going wrong some where

RTBUnformatedData.Text = str.ToString


this is on line 33

any help would be great thanks michael

This post has been edited by m_wylie85: 05 September 2011 - 10:27 AM

Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: tutorial about using DAT files

Posted 05 September 2011 - 10:35 AM

May it time you learnt about the skills of Debugging.
Was This Post Helpful? 0
  • +
  • -

#15 m_wylie85   User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: tutorial about using DAT files

Posted 05 September 2011 - 10:37 AM

ok will do thanks debugging like break points etc ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2