reading file LINE by LINE

I already built a New Character Page that you gill out information like name gender and class that auto-generates stats for the specific character (Stamina, health, mana, strength defence, agaility, and wisdom) and saves each of those stats as individual txt files and puts them in a folder depending on what you put in as your name. Now When I start my game I want to chose what account to play as. I am thinking when I save my character it saves annother textFile called CharacterList.txt and adds a line to that character list for whataver you put in as your name. so if you put in Shredder as your name. it would add a line in the txt file called shredder. and when I go to chose a character I would have a ListBox and each line of the list box would be a line of the txt file. I need to learn how to do that.




Answer this question

reading file LINE by LINE

  • Leon Ariaans

    Yea, i ran it in the debugger, no errors.  might be a version issue.

    Anyways, to auto-load, it's the same kinda file read/write, only without using a open dialog file.



        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AutoLoad("C:\RPG\DATA\User.txt")
        End Sub

        Private Sub AutoLoad(ByVal sFile As String)
            Dim sReader As IO.StreamReader
            sReader = IO.File.OpenText(sFile)

            lst_Characters.Items.Clear()
            Do Until sReader.EndOfStream
                lst_Characters.Items.Add(sReader.ReadLine)
            Loop
            sReader.Close()
            sReader.Dispose()
            sReader = Nothing
        End Sub

     


    Dustin.


  • KatyB

    Heya!!
    Funny how i randomly read your post... ehhehe
    Its cause im also trying to make some kind of RPG like that, so I thought if maybe we could exchange email or MSN So maybe we can help each other out and stuff...
    Currently Ive got a Database in SQL done, plus about 10 screeens with creating accounts, chars, and everything about that stuff..
    Now im working about some of the IG stuff, like inventory, player screen and status...

    If you to contact, just send me a email or add me in MSN Messenger.
    mateus_ms@hotmail.com
    Cya and good luck! :)

  • Peter Kryszak

    That's possible for your version.  I'm using version 8.0.50727.26,  and it has it included in the framework.

    If it gives you an error, then simply comment out that line.

    Dustin.


  • Doug4636xx

    Here's a sample to read/write a list to text file.
    on the form there's a listbox, a save button, a load button, a textbox for name entry, and a enter name button.



    Public Class Form1

        Private Sub cmd_LoadList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_LoadList.Click
            Dim dlg As New OpenFileDialog
            With dlg
                .DefaultExt = ".txt"
                .Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt"
                .InitialDirectory = Application.StartupPath
                .CheckFileExists = True
                If .ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
                    Dim sReader As IO.StreamReader
                    sReader = IO.File.OpenText(.FileName)

                    lst_Characters.Items.Clear()
                    Do Until sReader.EndOfStream
                        lst_Characters.Items.Add(sReader.ReadLine)
                    Loop
                    sReader.Close()
                    sReader.Dispose()
                    sReader = Nothing
                End If
            End With
            dlg.Dispose()
            dlg = Nothing
        End Sub

        Private Sub cmd_SaveList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_SaveList.Click
            Dim dlg As New SaveFileDialog
            With dlg
                .DefaultExt = ".txt"
                .Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt"
                .InitialDirectory = Application.StartupPath
                .OverwritePrompt = True
                If .ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
                    Dim sWriter As IO.StreamWriter

                    If IO.File.Exists(.FileName) Then IO.File.Delete(.FileName)
                    sWriter = IO.File.CreateText(.FileName)
                    For i As Integer = 0 To lst_Characters.Items.Count - 1
                        sWriter.WriteLine(lst_Characters.Items(i))
                    Next
                    sWriter.Close()
                    sWriter.Dispose()
                    sWriter = Nothing
                End If
            End With
            dlg.Dispose()
            dlg = Nothing
        End Sub

        Private Sub cmd_AddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_AddNew.Click
            If txtCharacterName.Text = "" Then
                MsgBox("Type in a name first")
                Exit Sub
            End If
            lst_Characters.Items.Add(txtCharacterName.Text)
            txtCharacterName.Text = ""
        End Sub
    End Class

     



    Dustin.


  • Rick1138

    Here's the same save code, only i removed the actual saving part into a new sub, and allowed you to pass a bool value to determine the text will get overwritten, or appended. (default to append now)



        Private Sub cmd_SaveList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_SaveList.Click
            Dim dlg As New SaveFileDialog
            With dlg
                .DefaultExt = ".txt"
                .Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt"
                .InitialDirectory = Application.StartupPath
                .OverwritePrompt = True
                If .ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
                    SaveToFile(.FileName, False)
                End If
            End With
            dlg.Dispose()
            dlg = Nothing
        End Sub

        Public Sub SaveToFile(ByVal sFile As String, Optional ByVal Overwrite As Boolean = False)
            Dim sWriter As IO.StreamWriter

            If Overwrite Then
                If IO.File.Exists(sFile) Then IO.File.Delete(sFile)
                sWriter = IO.File.CreateText(sFile)
            Else
                sWriter = IO.File.AppendText(sFile)
            End If
           
            For i As Integer = 0 To lst_Characters.Items.Count - 1
                sWriter.WriteLine(lst_Characters.Items(i))
            Next
            sWriter.Close()
            sWriter.Dispose()
            sWriter = Nothing
        End Sub

     


    Dustin.


  • fagrnada

    hey Dustin...did you try debugging that I get a error
    "Overload resolution failed because no 'Dispose' is accessible."
    sooo i just deleted those lines and ran it...
    the only problem is i have the saving part down...but I want it to load jusy 1 text file auto-matticly without the user having to search for it in a ListBox...
    C:\RPG\DATA\User.txt


  • KrisRicher

    worked great! how do I save to a txt file but instead of overwrite it add a new line in the txt file...

  • ZBizKiT

    In VB2005 the StreamReader object does not have a Public Dispose method, but its Close method "calls the Dispose method passing a true value."
    So there is no need for sWriter.Dispose() nor sReader.Dispose() code.


  • reading file LINE by LINE