Stream Reader I/O
Stream Reader I/O
Stream Reader I/O
Stream Reader I/O
Stream Reader I/O
Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O
Stream Reader I/O Stream Reader I/O
Stream Reader I/O
Go Back  Xtreme Visual Basic Talk > > > Stream Reader I/O


Reply
 
Thread Tools Display Modes
  #1  
Old 09-03-2011, 10:22 AM
dragon2000 dragon2000 is offline
Newcomer
 
Join Date: Sep 2011
Posts: 1
Default


Hi. I'm trying to read a file storing multiple lines into arrays, but its returning nil. Can anybody help me and tell me whats I'm doing wrong? Any help would be greatly appreciated.

This is the code I have:

Dim sfile As New System.IO.StreamReader("c:\users\sonyvaio2009\desktop\phbook.dat")
f = 1
fname(10000000) = sfile.ReadLine()
While (fname(f) <> "")
'f = f + 1
Console.WriteLine(fname(f)) : lname(f) = sfile.ReadLine()
Console.WriteLine(lname(f)) : add(f) = sfile.ReadLine()
Console.WriteLine(add(f)) : apt(f) = sfile.ReadLine()
Console.WriteLine(apt(f)) : city(f) = sfile.ReadLine()
Console.WriteLine(city(f)) : state(f) = sfile.ReadLine()
Console.WriteLine(state(f)) : zip(f) = sfile.ReadLine()
Console.WriteLine(zip(f)) : hphone(f) = sfile.ReadLine()
Console.WriteLine(hphone(f)) : wphone(f) = sfile.ReadLine()
Console.WriteLine(wphone(f)) : mphone(f) = sfile.ReadLine()
Console.WriteLine(mphone(f)) : email(f) = sfile.ReadLine()
Console.WriteLine(email(f)) : fname(f) = sfile.ReadLine()"
f = f + 1
End While
sfile.Close()
End If
Reply With Quote
  #2  
Old 09-28-2011, 10:08 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Ok haven't run the code but this doesn't look right to me...

f = 1
fname(10000000) = sfile.ReadLine()
While (fname(f) <> "")

If I am reading that right you have read a single line, and put it into the last slot of your string array fname.

So when you do your While <-> End While, and checked fname(1) where you didn't put anything it will be "" causing the rest of the While not to happen...

If you want to read everything into a string array (not sure why you would) but you need to do something that loads the string array first.
Reply With Quote
  #3  
Old 09-29-2011, 04:33 PM
snarfblam's Avatar
snarfblamStream Reader I/O snarfblam is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Apr 2005
Location: USA
Posts: 896
Default

Yep. Looks like that should be
Code:
fname(1) = sfile.ReadLine()
Not sure why you are using 1-based arrays (or treating them as so), but I guess that's beside the point.


Unless you expect the file may be excessively large, I think it would be easier to use the function File.ReadAllLines.
Code:
Dim Lines As String() = System.IO.File.ReadAllLines(filename)
This way you'll know off the bat how many lines there are, which makes it easier to detect something like an unexpected EOF.
__________________
C# _VB.NET _
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
Stream Reader I/O
Stream Reader I/O
Stream Reader I/O Stream Reader I/O
Stream Reader I/O
Stream Reader I/O
Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O Stream Reader I/O
Stream Reader I/O
Stream Reader I/O
 
Stream Reader I/O
Stream Reader I/O
 
-->