Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

In many parts of my program I save things to a file (usually .dat) in the following format:

 

Setting1        1
Setting2        0
Setting3        0

 

The spacing in between is actually a vbTab so when I read it in I use the split function to get the data I need.

 

Anyway my problem is that now I'm having a file saved in the following format:

 

Name          Rat

Level          2

Drops          Coins: 10-20
Bones
Raw meat

 

Once again the spacing is a vbTab and i write a blank line in between each. The problem is that for the Drops title there are 3 lines that need to be loaded but when I load the data into my program it only loads the Coins: 10-20 and leaves off the Bones and Raw meat.

 

Here is the code I am using:

 

        Dim FileName As String
       FileName = "Data.dat"
               Dim srBestiary As System.IO.StreamReader = New System.IO.StreamReader(FileName)
               Dim line, vals() As String
               Do
                   line = srBestiary.ReadLine
                   If line Is Nothing Then Exit Do
                   vals = line.Split(vbTab)
                   If vals(0) = "Name" Then
                       Me.txtName.Text = vals(1)
                   ElseIf vals(0) = "Level" Then
                       Me.txtLevel.Text = vals(1)
                   ElseIf vals(0) = "Drops" Then
                       Me.txtDrops.Text = vals(1)
                   End If
               Loop
               srBestiary.Close()
               srBestiary = Nothing

 

If you need to see the code im using to save the data to the file just let me know. Thanks!

 

Edit: Forgot to say this, but in the file that im loading from it will always have the format with Name, Level, and Drops.. the only things that change are what is after them.

Edited by Lanc1988
Posted

in the

ElseIf vals(0) = "Drops" Then
                       Me.txtDrops.Text = vals(1)
                   End If

you have to have an additional loop which checks on a vbNewLine (or just empty line). Before you encounter that (or a EndOfFile) you can add each sentence as an item.

i.e.

 

Dim FileName As String
       FileName = "Data.dat"
               Dim srBestiary As System.IO.StreamReader = New System.IO.StreamReader(FileName)
               Dim line, vals() As String
               Do
                   line = srBestiary.ReadLine
                   If line Is Nothing Then Exit Do
                   vals = line.Split(vbTab)
                   If vals(0) = "Name" Then
                       Me.txtName.Text = vals(1)
                   ElseIf vals(0) = "Level" Then
                       Me.txtLevel.Text = vals(1)
                   ElseIf vals(0) = "Drops" Then
' Add this piece here
while(line = srBestiary.ReadLine)
     if string.IsNullOrEmpty(line) exit while
     Me.txtDrops.Text = line
end while
                   End If
               Loop
               srBestiary.Close()
               srBestiary = Nothing

 

however you should also append the text or something, since now you are replacing it and thus the textbox (assuming txtDrops is a textbox) will only read the last line of the document, thus 'Raw Meat'.

You can change this for example by replacing

Me.txtDrops.Text = line

with

Me.txtDrops.Text &= vbNewLine & line

 

~DP

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted (edited)

I tried the code you posted but it is giving me problems with this line:

 

if string.IsNullOrEmpty(line) exit while

 

It says statement must end with a matching End If and also End of Statement Expected.

 

So I tried to replace it with the following which gets rid of the error but doesnt load any of the information for Drops:

 

if string.IsNullOrEmpty(line) then exit while

 

 

 

Edit: just did a small test and it is never entering the While loop but im not sure what condition needs to be there instead of while(line = srBestiary.ReadLine)

Edited by Lanc1988
Posted

using visual studio 2008 also.. it works in that it doesnt cause any errors when the program is running, but it doesnt work because the while loop never happens

 

(tested it by putting a label on the form and at the beginning of the while loop i put text in the label but the text never gets put into the label)

  • 4 weeks later...
Posted

Just wondering if anyone could help me with a problem I am having. I need to have a lot o webpages that pull a line of text from a txt file so that when the file is changed, all the HTML files will have the new info when they start.

 

Any ideas? Please let me know.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...