26 Replies - 19973 Views - Last Post: 06 September 2011 - 05:46 PM
#1
tutorial about using DAT files
Posted 01 September 2011 - 10:23 AM
I am pretty sure you and use stream reader and writer form the IO but what about formatting. Any help would be great
thanks
Replies To: tutorial about using DAT files
#2
Re: tutorial about using DAT files
Posted 01 September 2011 - 11:47 AM
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.
#3
Re: tutorial about using DAT files
Posted 01 September 2011 - 01:01 PM
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.
#4
Re: tutorial about using DAT files
Posted 01 September 2011 - 02:26 PM
#5
Re: tutorial about using DAT files
Posted 01 September 2011 - 02:34 PM
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
#6
Re: tutorial about using DAT files
Posted 01 September 2011 - 03:41 PM
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
#7
Re: tutorial about using DAT files
Posted 02 September 2011 - 12:11 AM
#8
Re: tutorial about using DAT files
Posted 04 September 2011 - 01:11 PM
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
#9
Re: tutorial about using DAT files
Posted 04 September 2011 - 03:06 PM
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.
#10
Re: tutorial about using DAT files
Posted 04 September 2011 - 03:36 PM
#11
Re: tutorial about using DAT files
Posted 04 September 2011 - 04:17 PM
#12
Re: tutorial about using DAT files
Posted 04 September 2011 - 05:12 PM
#13
Re: tutorial about using DAT files
Posted 05 September 2011 - 10:27 AM
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
#14
Re: tutorial about using DAT files
Posted 05 September 2011 - 10:35 AM
#15
Re: tutorial about using DAT files
Posted 05 September 2011 - 10:37 AM

New Topic/Question
Reply



MultiQuote



|