|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
StringReaders and DataWriters and FileStreams, oh, my!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 Kristian Frost wrote:
> My problem is that I want to read some whole lines from some legacy saved <snip>> 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 <snip>> are, of course, all of many varying lengths. > > their legacy code system as it fails to "see" the difference between a Assuming your linebreak is a carriage return and linefeed, then you can> comma and a linebreak. > 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. 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/
Problem calling a Sub in another form
WebBrowser - Excel processs till running Random Numbers Not Being Random Q: Copying part of a table Specifiy that i'm referring to a class, not a member PrintDocument and HasMorePages issue Write commands to the COM port TextFieldParser - reading tab delimited file Refresh/Blinking Problems with VB.NET Application Calling Subprocedures |
|||||||||||||||||||||||