473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StringReaders and DataWriters and FileStreams, oh, my!

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/
Sep 21 '06 #1
2 1109
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.

Sep 21 '06 #2
Cheers, that was exactly what I was looking for
KF
On Thu, 21 Sep 2006 18:19:15 +0100, Chris Dunaway <du******@gmail.com
wrote:
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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Sep 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J | last post by:
Is it possible to achieve this without PInvoking?
2
by: Suzanne | last post by:
Hi all, I'm attempting to save images (that are in jpeg format) to a database column of type image. I've read over the Microsoft Knowledge Base articles for this and I can get things working...
10
by: Nick N. | last post by:
Hi all, I just started to play around with VB.Net. I currenlty do Powerbuilder work and was wondering how database connection management works in VB.net. My applications typically pop-up a...
4
by: monade | last post by:
Hi, i have a file stream std::ofstream file and some data std::string data to serialize in a performance sensitive code section: file << data << '\n' I know that the data will be flushed when...
6
by: al jones | last post by:
I picked a good project to try to learn to use VS - and up till now everything has worked the way I expect. The code is from a VS.net class, which I picked up on the web, from which I've extracted...
5
by: news.microsoft.com | last post by:
Hello, what is the most performant size for the byte array for reading/writing using 2 filestreams? example code: Dim bytearrayinput(4095) As Byte Dim rdlen As Long = 0 Dim totlen As Long...
0
by: kevlar | last post by:
I am having some problems reading xml into a dataset and having it set up the datatables within it properly. i serialize an object into xml and read that xml into the dataset and the code i am...
7
by: Bob Weiner | last post by:
Hi, I'm an IT guy who likes to think he can program. I have a generic question about filestreams. I'm putting together an application and need to write certain activities out to a log file. ...
4
by: per9000 | last post by:
Hi python people, I am trying to figure out the best way to encrypt files in python. I've build a small script (see below) that encrypts the ubuntu 7.04 iso file in 2 minutes (I like python :)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.