StringReaders and DataWriters and FileStreams, oh, my!

K

Kristian Frost

Silly title, sorry.

Anyway, I'm thoroughly confused about the whole new "streams" business and
it's very late in my local day, so I wondered if any of you in other parts
of the world could solve this one for me while I go home and sleep.

My problem is that I want to read some whole lines from some legacy saved
text-based datafiles and then read individual comma-separated values from
the lines into an ObjectRecogniser object I have mostly-working.

So the data-line:
foo, bar, helloworld, 1

will be recognised by my ObjectRecogniser as an object of type 1 with data
"foo", "bar" and "helloworld", which it does fine as far as that goes, but
it can't process the data until it sees that 1, and I can't guarantee that
the 1 will be the data identifier without seeing the linebreak. The lines
are, of course, all of many varying lengths.

The filereader for our legacy LoadFile function uses heavily structured,
complex and highly breakable code to read the exact data it is expecting
at exactly the right point in the file with hundreds and hundreds of
individual "Input #File, data" commands. It puts appropriate linebreaks in
during the save process so the data is fairly human-readable, but the
loading process is reading in Strings and not giving any indication as to
what the separators used were.
We're trying to move to a more versatile, expandable system which will
recognise objects from their savelines and act accordingly, so I can't use
their legacy code system as it fails to "see" the difference between a
comma and a linebreak.

Can anyone tell me how I could read the file a line at a time and feed the
line to some new function that would read the comme-separated values?
I'm thinking that these "stream" things are the way to go, but they're all
very much a mystery to me at this point.

Even some basic advice would be welcome.

Cheers,
KF
 
C

Chris Dunaway

Kristian said:
My problem is that I want to read some whole lines from some legacy saved
text-based datafiles and then read individual comma-separated values from
the lines into an ObjectRecogniser object I have mostly-working.

So the data-line:
foo, bar, helloworld, 1

the 1 will be the data identifier without seeing the linebreak. The lines
are, of course, all of many varying lengths.

their legacy code system as it fails to "see" the difference between a
comma and a linebreak.

Assuming your linebreak is a carriage return and linefeed, then you can
use a StreamReader to read the file line by line and use split to split
it apart:

Dim rdr As New StreamReader("c:\filename.ext")

Dim line As String = rdr.ReadLine()
While line IsNot Nothing

Dim lineParts As String() = line.Split(","c)

'lineParts is an array that contains foo, bar, helloworld, and 1
'do something with it here

End While

rdr.Close()

rdr.Dispose()

NOTE: This is air code, so watch for typos.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top