Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hey

 

i have this code to read text from a textfile with StreamReader

what i want to do is:

 

- be able to sort out each CHARACTER - not line, the

streamreader reads

 

- find out what line number i am currently reading

 

heres what i got

 

Imports System
Imports System.IO

'the code below is in form1.load

Dim srdr As StreamReader = New StreamReader("map/test/test.txt") 


       Dim ln String

       Do
           line = srdr.ReadLine()
           MessageBox.Show(ln)
       Loop Until ln Is Nothing

       srdr.Close()


 

thanks

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Administrators
Posted

If you are reading it a line at a time you could then keep track of the line count based on each readline and treat the string as a character array.

 

Dim srdr As StreamReader = New StreamReader("map/test/test.txt")

Dim LineCount As Integer
Dim ln As String

Do
LineCount += 1
ln = srdr.ReadLine()
MessageBox.Show(ln)
Dim ch() As Char = ln.ToCharArray

For Each c As Char In ch
	MessageBox.Show(c.ToString())
Next
Loop Until ln Is Nothing

srdr.Close()

 

or similar

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted (edited)

Just my lazy naming convention;)

ch() is the string turned into an array of characters, c will contain an individual character. The for each c as char in ch just loops through them one character at a time.

The syntax is just a shorthand way of doing for each loops in VS.Net 2003

 

For Each c As Char In ch
       MessageBox.Show(c.ToString())
Next

'same as 
dim c as char
for each c in ch
       MessageBox.Show(c.ToString())
Next

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

oh vs.net 03 :-p ihave 02

 

i am experiencing 1 problem though

when it gets to the very end of the textfile,

Dim ch() As Char = ln.ToCharArray

gives an error:

An unhandled exception of type 'System.NullReferenceException' occurred in RPG.exe

 

Additional information: Object reference not set to an instance of an object.

 

im guessing that since its at the very end, nothing is there it cannot convert it("nothing") to a char array

 

any suggestions? please help - thanks

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Administrators
Posted

You could use a try ... catch block around the line or just check it first

 

Dim srdr As StreamReader = New StreamReader("map/test/test.txt")

Dim LineCount As Integer
Dim ln As String

Do
   LineCount += 1
   ln = srdr.ReadLine()
   MessageBox.Show(ln)
   'May be a bit of a nasty hack but should work.
   If ln.ToCharArray Is Nothing Then Exit Do
   
   Dim ch() As Char = ln.ToCharArray

   For Each c As Char In ch
       MessageBox.Show(c.ToString())
   Next
Loop Until ln Is Nothing

srdr.Close()

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

nice

 

i never knew you could "If ln.ToCharArray Is Nothing Then Exit Do"

 

so u can exit loops at anytime?

if u wanted to exit an If loop you would say Exit If rihgt?

- thers always something new you could learn :-d

 

thanks a lot man

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • *Experts*
Posted

There's no such thing as an "If loop", but yes, you can exit any

type of loop by using Exit (name of loop).

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

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...