Home All Groups Group Topic Archive Search About

StringReaders and DataWriters and FileStreams, oh, my!

Author
21 Sep 2006 3:46 PM
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

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Author
21 Sep 2006 5:19 PM
Chris Dunaway
Kristian Frost wrote:
> 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
>

<snip>

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

<snip>

> 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.
Author
22 Sep 2006 7:29 AM
Kristian Frost
Cheers, that was exactly what I was looking for
KF


On Thu, 21 Sep 2006 18:19:15 +0100, Chris Dunaway <dunaw***@gmail.com>  =

wrote:

Show quoteHide quote
> Kristian Frost wrote:
>> 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
>>
>
> <snip>
>
>> the 1 will be the data identifier without seeing the linebreak. The  =

>> lines
>> are, of course, all of many varying lengths.
>>
>
> <snip>
>
>> 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 ca=
n
> use a StreamReader to read the file line by line and use split to spli=
t
> it apart:
>
> Dim rdr As New StreamReader("c:\filename.ext")
>
> Dim line As String =3D rdr.ReadLine()
> While line IsNot Nothing
>
>     Dim lineParts As String() =3D 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.
>



-- =

Using Opera's revolutionary e-mail client: http://www.opera.com/mail/