Lanc1988 Posted August 10, 2009 Posted August 10, 2009 (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 August 10, 2009 by Lanc1988 Quote
DPrometheus Posted August 10, 2009 Posted August 10, 2009 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 Quote 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
Lanc1988 Posted August 10, 2009 Author Posted August 10, 2009 (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 August 10, 2009 by Lanc1988 Quote
Administrators PlausiblyDamp Posted August 10, 2009 Administrators Posted August 10, 2009 What version of VB are you using? Works fine in 2008 but haven't got any older versions to check... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Lanc1988 Posted August 10, 2009 Author Posted August 10, 2009 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) Quote
Lanc1988 Posted August 10, 2009 Author Posted August 10, 2009 Heres the zipped up project if that helps you see what I mean: (also.. if the blank lines that I write in between are the problem, i can change that to anything.. it could be a space, a dash "-" or whatever)WindowsApplication2.zip Quote
Administrators PlausiblyDamp Posted August 10, 2009 Administrators Posted August 10, 2009 Try Do While Not String.IsNullOrEmpty(line) line = srBestiary.ReadLine Me.txtDrops.Text &= vbNewLine & line Loop Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Lanc1988 Posted August 10, 2009 Author Posted August 10, 2009 Works perfectly.. thanks for everyones help! Quote
skolapper Posted September 4, 2009 Posted September 4, 2009 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. Quote
Leaders snarfblam Posted September 4, 2009 Leaders Posted September 4, 2009 This should go in its own thread. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.