Guest locutus Posted September 9, 2002 Posted September 9, 2002 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 Quote
*Experts* Bucky Posted September 9, 2002 *Experts* Posted September 9, 2002 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* Derek Stone Posted September 9, 2002 *Gurus* Posted September 9, 2002 If you're worried about speed the ReadToEnd method isn't a wise choice. Use ReadBlock instead. Quote Posting Guidelines
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.