[FIXED][StreamWriting/Reading] String problems

Discuss anything programming related.
Post Reply
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

[FIXED][StreamWriting/Reading] String problems

Post by Click16 »

Ok, I am working with a StreamWriter. For the sake of demonstration, I will create a new app just for stream reading and writing.

Also, I'm using VB. This is because I started a project a while ago. I didn't know C# at the time, so I am continuing the project using VB. (I'm sure I can figure it out if C# is used though.)

This is the app I made for this:
Image


PROBLEM:
I will add this code to the button1.click event. (Button1 = Write)

Code: Select all

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Setting the File up
        If My.Computer.FileSystem.FileExists("C:\WriteFile.txt") = False Then
            MessageBox.Show("You Dumbass, make a text file called ''WriteFile'' in your C:\ Directory! >_<")
        Else

        End If

        ' Open a file for writing  
        Dim FILENAME As String = ("C:\WriteFile.txt")

        ' Create a new StreamWriter as nsr 
        Dim nsr As StreamWriter
        nsr = File.AppendText(FILENAME)
        'Write the contents of textbox1 to a new line.  
        nsr.WriteLine(TextBox1.Text)

        ' Close the stream  
        nsr.Close()

    End Sub
So I write the following lines of text into the text file using my new app.

Image

It writes!

Now lets get it to read.

I put this code into the button2.click event:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.text = txt
        
        'Closing the Stream Reader
        nsr.Close()
    End Sub
I Debug and click read.

Image

It works, But lets say I want to put each line as a seperate Object in a combobox. So I replace textbox2 with combobox1.

I changed the code around to try to get it to read correctly.

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.Items.Clear()
        TextBox2.Items.Add(txt)

        'Closing the Stream Reader
        nsr.Close()
    End Sub
Image


SOLUTION CODE:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Adding Items to the Combobox
        Do Until nsr.EndOfStream
            Me.TextBox2.Items.Add(nsr.ReadLine)
        Loop

        'Closing the StreamReader
        nsr.Close()
    End Sub
Image

Anyone can use this now... Don't delete this topic.

-Click
Attachments
StreamReadWriteTutorial.rar
Stream-Read-Write Tutorial
(111.41 KiB) Downloaded 275 times
Last edited by Click16 on Fri Jul 03, 2009 7:27 am, edited 1 time in total.
Image
User avatar
troymac1ure
Keeper of Entity
Posts: 1282
Joined: Sat Aug 09, 2008 4:16 am
Location: British Columbia, Canada, eh
Contact:

Re: [StreamWriting/Reading] String problems

Post by troymac1ure »

Click16 wrote: It works, But lets say I want to put each line as a seperate Object in a combobox. So I replace textbox2 with combobox1.

I changed the code around to try to get it to read correctly.

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String = nsr.ReadToEnd()
        'Adding the text to textbox2
        TextBox2.Items.Clear()
        TextBox2.Items.Add(txt)

        'Closing the Stream Reader
        nsr.Close()
    End Sub
Image

It reads, But does not display the items as separate collections. How can I separate them?

Thanks to all who help!

-Click
Haven't worked a whole lot with VB, but you should be able to do it with .ReadLine()
Something like so:

Code: Select all

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Creating a StreamRead
        Dim nsr As New StreamReader("C:\WriteFile.txt")

        'Simplifying the text
        Dim txt As String
        TextBox2.Items.Clear()
        while ((txt = nsr.ReadLine()) != null)
        {
            'Adding the text to textbox2
            TextBox2.Items.Add(txt)
        }
        'Closing the Stream Reader
        nsr.Close()
    End Sub
Try this. If not it should be along these lines.
User avatar
neodos
Posts: 1493
Joined: Sun Dec 09, 2007 8:58 pm

Re: [FIXED][StreamWriting/Reading] String problems

Post by neodos »

This is useful to me, i need to learn reading files, can you post the full source?

Thanks
User avatar
JacksonCougar
Huurcat
Posts: 2460
Joined: Thu Dec 06, 2007 11:30 pm
Location: Somewhere in Canada

Re: [FIXED][StreamWriting/Reading] String problems

Post by JacksonCougar »

Damnit, posted in the wrong one. This is all your fault Click.
User avatar
neodos
Posts: 1493
Joined: Sun Dec 09, 2007 8:58 pm

Re: [FIXED][StreamWriting/Reading] String problems

Post by neodos »

Lmao, poor Click.
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: [FIXED][StreamWriting/Reading] String problems

Post by Click16 »

neodos wrote:Lmao, poor Click.
:lol:

Ok, I will put up the full source.

Source is UP!
Image
User avatar
neodos
Posts: 1493
Joined: Sun Dec 09, 2007 8:58 pm

Re: [FIXED][StreamWriting/Reading] String problems

Post by neodos »

Thanks for the source, lol the pop up messages were unnecessary though :XD:
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: [FIXED][StreamWriting/Reading] String problems

Post by Click16 »

neodos wrote:Thanks for the source, lol the pop up messages were unnecessary though :XD:
I know, i thought it would be funny.
Image
Post Reply