Jump to content
Xtreme .Net Talk

im new to .net and my book doesn't cover this simple request for reading in files


Recommended Posts

Guest locutus
Posted

Hi

I have just started using vb and the .net enviroment and I am going through my teach yourself book . I decided to have a break and create a little application to practise some of the techniques I had learned.

 

I am trying to open a text file and loop through and read each line of the file into a holder of some type ( will prob use an array for the sake of using them in my example )

 

heres what I have so far

dim fsout as system.io.filestream

dim filename as string

dim oreader as system.io.streamreader

dim scontents as string

dim bexists as boolean

 

filename = "c:\login.txt"

bexists = system.io.file.exists(filename)

 

try

'open the file

fsout = new system.io.filestream(filename,system.io.filemode.openorcreate,system.io.fileaccess.read)

 

oreader = new system.io.streamreader(fsout)

'think I need a while in here to loop

scontents = oreader.readline

 

this is what I have so far , all I want to do is loop through to the end of the file and be able to assign each loop through to a holder of some type , but this new approach in .net is very confusing and I am struggling to get my head around it.

Would anyone be able to help with this small learning example of mine . If you supply the code to help could you try and explain what is going on also with the code

 

Many thx for your time

locutus

  • *Experts*
Posted

To make raeding the stream easier, you should declare a new

StringReader object to read from the file stream. In the declaration

of your form, add this:

 

Dim sLines() As String

 

Then, after opening the file, read the entire thing, and then use

Split to divide it up into lines:

 

scontents = oreader.ReadToEnd

sLines = Split(scontents, vbCrLf) 
' You could also use sLines = scontents.Split(vbCrLf) if desired

 

Now sLines is an array of strings that holds each lines' text, and

you can now loop through them as you like.

 

HTH

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