PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Query about how to use StreamWriter.-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] Query about how to use StreamWriter.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Resolved [RESOLVED] Query about how to use StreamWriter.

    I have run into an issue where I need a button click to sort through a text file. It's supposed to sort people's names and their blood pressure levels and write it on another text file. This is the code I have so far, what else do i need to add to do so?

    Public Shared _intSizeOfArray As Integer = 15
    Public Shared _strPatientName(_intSizeOfArray) As String
    Public Shared _strBloodPressure(_intSizeOfArray) As Integer
    Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click

    ' This procedure sorts the list of patients into two categories. Those with systolic levels under 120
    ' and those with 120 and above, placing them on a list

    Dim strPatientName(15) As String
    Dim strBloodPressure(15) As String
    Dim objWriter As New IO.StreamWriter("f:\Contact.txt")
    Dim intCount As Integer

    For intCount = 0 To (strPatientName.Length - 1)
    If IO.File.Exists("f:\contact.txt") Then
    ' Write the file line by line until the file is completed

    objWriter.WriteLine(_strPatientName(intCount))
    objWriter.WriteLine(_strBloodPressure(intCount))

    End If

    Next

    ' The file is closed
    objWriter.Close()

    End Sub

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,626

    Re: Query about how to use StreamWriter.

    Ok, there are a number of things that are wrong in your approach. You shouldn't be using separate arrays for a person's name and their blood pressure. You use an array of a particular class for that like this:-
    vbnet Code:
    1. '
    2. Public Class Person
    3.  
    4.     Public Property Name As String
    5.     Public Property BloodPressure As String
    6.  
    7. End Class
    The above is an example of how the class might look.

    Here's an example of how to use an array of this class:-
    vbnet Code:
    1. '
    2.         Dim people As Person() = New Person(2) {}
    3.  
    4.         people(0) = New Person With {.BloodPressure = "100", .Name = "John Hick"}
    5.         people(1) = New Person With {.BloodPressure = "120", .Name = "Mary Hawkings"}
    6.         people(2) = New Person With {.BloodPressure = "140", .Name = "Jon Snow"}

    As for sorting. You're gonna have to explain that better. I don't really understand how you want to sort this. In particular, I want to know how you're going to describe a person's blood pressure. In my example, I just use a number but I know there's a bit more to blood pressure than that. I need to know how YOU actually represent blood pressure in code.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: Query about how to use StreamWriter.

    The application is set up where the user links a text file containing a list of patients names and blood pressures, for example: Appleseed, John 210. Then if the blood pressure is above, say 130, it writes the name and blood pressure on a separate text file with the click of a button.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,626

    Re: Query about how to use StreamWriter.

    So to be clear, the list of names and their corresponding blood pressure comes from a file? So you want to read from a file and write to another file? Is that correct?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: Query about how to use StreamWriter.

    Yes that's correct.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: Query about how to use StreamWriter.

    Once you have the two arrays populated, you can call Array.Sort to order both arrays based on the values in one of them. You can then create a StreamWriter and use a For loop to iterate over the two arrays, writing a value from each on each iteration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,626

    Re: Query about how to use StreamWriter.

    Here's an example:-
    vbnet Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         'Will hold records of all the people
    8.         Dim people As New List(Of Person)
    9.  
    10.         'Original file with all records
    11.         Dim recordsFile As String = "C:\bp.txt"
    12.  
    13.         'The file with the filtered records
    14.         Dim filteredFile As String = "c:\bp_filtered.txt"
    15.  
    16.         Using fs As New FileStream(recordsFile, FileMode.Open, FileAccess.Read, FileShare.Read)
    17.  
    18.             'Its a text file so we use the StreamReader to read the lines
    19.             Using sr As New StreamReader(fs, System.Text.Encoding.UTF8)
    20.  
    21.                 'Go until we reach the end of the file. Happens after
    22.                 'we read the last line
    23.                 Do Until sr.EndOfStream
    24.  
    25.                     Dim line As String = sr.ReadLine
    26.  
    27.                     'Divide each line into 3 sections, one for first name, last name
    28.                     'and blood pressure
    29.                     Dim lineSections As String() = line.Split(" "c)
    30.  
    31.                     'Create a Person object from the info from the line sections
    32.                     Dim p As New Person(lineSections(1), lineSections(0).Replace(",", ""), CInt(lineSections(2)))
    33.  
    34.                     'Add person to the list
    35.                     people.Add(p)
    36.                 Loop
    37.             End Using
    38.         End Using
    39.  
    40.         'Create a new file which will contain a filtered list of people
    41.         Using fs As New FileStream(filteredFile, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)
    42.  
    43.             'Use a StreamWriter so we can write a text file easily
    44.             Using tw As New StreamWriter(fs, System.Text.Encoding.UTF8)
    45.  
    46.                 'Only include people who's blood pressure is over 130.
    47.                 'ie: Filter out people who's blood pressure is under 131
    48.                 For Each p As Person In people.Where(Function(p1) p1.BloodPressure > 130)
    49.  
    50.                     'Write person's info in the same format as the original
    51.                     'text file with the people's records
    52.                     tw.WriteLine(p.LastName + ", " + p.FirstName + " " + p.BloodPressure.ToString)
    53.                 Next
    54.             End Using
    55.         End Using
    56.     End Sub
    57.  
    58.  
    59.    
    60.  
    61. End Class
    62.  
    63.  
    64. '
    65. Public Class Person
    66.  
    67.     Public Sub New()
    68.     End Sub
    69.  
    70.     Public Sub New(ByVal fn As String, ByVal ln As String, ByVal bp As Integer)
    71.         Me.FirstName = fn
    72.         Me.LastName = ln
    73.         Me.BloodPressure = bp
    74.     End Sub
    75.  
    76.     Public Property FirstName As String
    77.     Public Property LastName As String
    78.     Public Property BloodPressure As Integer
    79.  
    80. End Class

    The above assumes your source file has a specific format, one that looks like this:-
    Code:
    Hex, John 100
    Sue, Mary 130
    Conrad, Adrian 135
    Dorn, Peter 110
    Ulyses, Alice 100
    Viktor, Kayn 160
    Matthew, Lilith 185
    Using the above text file as the source file, my code will produce this text file:-
    Code:
    Conrad, Adrian 135
    Viktor, Kayn 160
    Matthew, Lilith 185
    As you can see form the above, it writes a text file with only those people with a blood pressure above 130.

    Note that the text format of the source must match what your code expects. In my case, I wrote the code to expect a format of [string] [string] [number], each separated by one space. Any deviation from that and the code will fail. So you must adjust the code to suit any peculiarities in the text format that might arise in your case.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: Query about how to use StreamWriter.

    Thanks I appreciate the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width