Reading a file
Reading a file
Reading a file
Reading a file
Reading a file
Reading a file Reading a file Reading a file Reading a file Reading a file Reading a file Reading a file Reading a file
Reading a file Reading a file
Reading a file
Go Back  Xtreme Visual Basic Talk > > > Reading a file


Reply
 
Thread Tools Display Modes
  #1  
Old 07-01-2011, 12:10 PM
ScottCFR ScottCFR is offline
Newcomer
 
Join Date: Jul 2011
Posts: 3
Default Reading a file


I've been working on a program for a few weeks now, but I can't seem to find an answer to my problem. I need to read a file (line by line) and then, make the check needed, then go back to the file and get the next line. How is this done?

Regards,
ScottCFR
Reply With Quote
  #2  
Old 07-01-2011, 12:30 PM
AtmaWeapon's Avatar
AtmaWeaponReading a file AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

The System.IO.StreamReader class is what you want. You can read the documentation for intimate details; I'll provide an overview.

For the most simple case, you just want to open a file using its name. You can use the (String) constructor to pull this off:
Code:
Dim reader As New StreamReader(<your file name>)
Next, you want to loop as long as there are lines in the file. You have two choices. The ReadLine() method reads a line and returns it or returns Nothing if the reader is at the end of the file. It's more common to check the EndOfStream property:
Code:
While Not reader.EndOfStream
Now you want to read a line and do something with that line; I've already talked about ReadLine() so you can imagine that has something to do with it:
Code:
    Dim line As String = reader.ReadLine()
    <do what you need to do with the line>
End While
Slow down though, there's one last thing. Since the StreamReader represents a file handle, you need to tell Windows you're done with it ASAP. You do this with the Close() or Dispose() method. Due to some technicalities that aren't worth discussing, it's best to use a Using statement which does the cleanup for you automatically:
Code:
Using reader As New StreamReader(<your file name>)
    While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        <do what you need to do with the line>
    End While
End Using
Someone might point out the System.IO.File.ReadAllLines() method's a quicker way to do this. It's true. But it has to load the entire file, and if your file's big that can take a while and use up memory. The "long" way using StreamReader is more efficient for large files and gives you a little more power. But if the file's small and you don't mind working with all of the lines in an array, feel free to use the shortcut.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-01-2011, 01:38 PM
ScottCFR ScottCFR is offline
Newcomer
 
Join Date: Jul 2011
Posts: 3
Default

After I'm done with one line, that will advance to another. Correct?
Reply With Quote
  #4  
Old 07-01-2011, 03:02 PM
AtmaWeapon's Avatar
AtmaWeaponReading a file AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Yes. (It would have been much faster to try it! It's also clearly stated in the documentation.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 07-01-2011, 03:54 PM
ScottCFR ScottCFR is offline
Newcomer
 
Join Date: Jul 2011
Posts: 3
Default

I did try it, as I got bored. Thanks for your help, it work'd perfect.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading a file
Reading a file
Reading a file Reading a file
Reading a file
Reading a file
Reading a file Reading a file Reading a file Reading a file Reading a file Reading a file Reading a file
Reading a file
Reading a file
 
Reading a file
Reading a file
 
-->