By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,822 Members | 729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,822 IT Pros & Developers. It's quick & easy.

Using streams how do I write and then read a set of variables

P: n/a
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:

string1,string2,string3

Then read them later.

Suppose I want to write and then read:
string1, integer1, double1

Can't I have each read/write use one line in the text file (probably comma
separated)?

I've been reading but can only find out how to write one thing on a line.

Thanks in advance

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?

A BinaryWriter & BinaryReader prefix strings with their length, while other
types (Integers, Doubles & such) are fixed sizes so what you write is what
you read.

Dim writer As BinaryWriter
writer.Write(string1)
writer.Write(integer1)
writer.Write(double1)

Dim reader As BinaryReader
string1 = reader.ReadString()
integer1 = reader.ReadInt32()
double1 = reader.ReadDouble()
Unfortunately TextWriter simply writes text, with NO formatting information.
Which means that TextReader has no parsing abilities.

You would need to manually write commas between the fields with TextWriter,
then manually parse the commas when you read them back...

I don't have any link handy, I would think a FormattedReader &
FormattedWriter might be useful set of base classes to define, along with
derived versions for FormattedString & FormattedStream, unfortunately I do
not know of any predefined right now...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uC**************@TK2MSFTNGP11.phx.gbl...
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:

string1,string2,string3

Then read them later.

Suppose I want to write and then read:
string1, integer1, double1

Can't I have each read/write use one line in the text file (probably
comma separated)?

I've been reading but can only find out how to write one thing on a line.

Thanks in advance


Nov 21 '05 #2

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?
I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks


A BinaryWriter & BinaryReader prefix strings with their length, while
other types (Integers, Doubles & such) are fixed sizes so what you write
is what you read.

Dim writer As BinaryWriter
writer.Write(string1)
writer.Write(integer1)
writer.Write(double1)

Dim reader As BinaryReader
string1 = reader.ReadString()
integer1 = reader.ReadInt32()
double1 = reader.ReadDouble()
Unfortunately TextWriter simply writes text, with NO formatting
information. Which means that TextReader has no parsing abilities.

You would need to manually write commas between the fields with
TextWriter, then manually parse the commas when you read them back...

I don't have any link handy, I would think a FormattedReader &
FormattedWriter might be useful set of base classes to define, along with
derived versions for FormattedString & FormattedStream, unfortunately I do
not know of any predefined right now...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uC**************@TK2MSFTNGP11.phx.gbl...
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:

string1,string2,string3

Then read them later.

Suppose I want to write and then read:
string1, integer1, double1

Can't I have each read/write use one line in the text file (probably
comma separated)?

I've been reading but can only find out how to write one thing on a line.

Thanks in advance



Nov 21 '05 #3

P: n/a
Just Me,
I'd like the file to be a .txt file If you want a .txt file, then you need (should) use a TextWriter,
specifically StreamWriter.
I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree? Rather then concat the strings, I normally write the three strings & the
seperators to the StreamWriter. (eliminating any potential for temporary
strings cluttering the GC)...

Something like:
Dim writer As StreamWriter
writer.Write(string1)
writer.Write(","c)
writer.Write(integer1)
writer.Write(","c)
writer.Write(double1)
writer.WriteLine()

Of course the above may be in a loop, especially if the variables are coming
from a DataTable or any object via reflection...

Alternatively I've used the format parameter on TextWriter.WriteLine to
format the variables, something like:

Const format As String = "{0}, {1}, {2}"
writer.WriteLine(format, string1, integer1, double1)

The format parameter is more useful for internationalization & including
other text in the line...
before write, and then use split to separate then after read. Agree? I normally use String.Split to read the file, however you can have problems
when the fields being written include the field delimiter or quoted
strings... I have not worked out a RegEx to use with RegEx.Split to more
intelligently split the fields...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:eh**************@TK2MSFTNGP15.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?


I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks

<<snip>>
Nov 21 '05 #4

P: n/a
It definitely helps
Thanks

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eT**************@tk2msftngp13.phx.gbl...
Just Me,
I'd like the file to be a .txt file

If you want a .txt file, then you need (should) use a TextWriter,
specifically StreamWriter.
I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

Rather then concat the strings, I normally write the three strings & the
seperators to the StreamWriter. (eliminating any potential for temporary
strings cluttering the GC)...

Something like:
Dim writer As StreamWriter
writer.Write(string1)
writer.Write(","c)
writer.Write(integer1)
writer.Write(","c)
writer.Write(double1)
writer.WriteLine()

Of course the above may be in a loop, especially if the variables are
coming from a DataTable or any object via reflection...

Alternatively I've used the format parameter on TextWriter.WriteLine to
format the variables, something like:

Const format As String = "{0}, {1}, {2}"
writer.WriteLine(format, string1, integer1, double1)

The format parameter is more useful for internationalization & including
other text in the line...
before write, and then use split to separate then after read. Agree?

I normally use String.Split to read the file, however you can have
problems when the fields being written include the field delimiter or
quoted strings... I have not worked out a RegEx to use with RegEx.Split to
more intelligently split the fields...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:eh**************@TK2MSFTNGP15.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?


I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks

<<snip>>

Nov 21 '05 #5

P: n/a
Just Me,
The XmlCsvReader might be useful for what you are attempting.

http://www.gotdotnet.com/Community/U...B-57A7DBBEBAE0

It reads a formatted file (such as CSV) as "Xml", this Xml should then be
easily parsed into your string1, integer1, double1 variables...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
It definitely helps
Thanks

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eT**************@tk2msftngp13.phx.gbl...
Just Me,
I'd like the file to be a .txt file

If you want a .txt file, then you need (should) use a TextWriter,
specifically StreamWriter.
I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

Rather then concat the strings, I normally write the three strings & the
seperators to the StreamWriter. (eliminating any potential for temporary
strings cluttering the GC)...

Something like:
Dim writer As StreamWriter
writer.Write(string1)
writer.Write(","c)
writer.Write(integer1)
writer.Write(","c)
writer.Write(double1)
writer.WriteLine()

Of course the above may be in a loop, especially if the variables are
coming from a DataTable or any object via reflection...

Alternatively I've used the format parameter on TextWriter.WriteLine to
format the variables, something like:

Const format As String = "{0}, {1}, {2}"
writer.WriteLine(format, string1, integer1, double1)

The format parameter is more useful for internationalization & including
other text in the line...
before write, and then use split to separate then after read. Agree?

I normally use String.Split to read the file, however you can have
problems when the fields being written include the field delimiter or
quoted strings... I have not worked out a RegEx to use with RegEx.Split
to more intelligently split the fields...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:eh**************@TK2MSFTNGP15.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Just Me,
Are you using a TextWriter or a BinaryWriter to write to the Stream?

I don't know.

Actually in this instance I need to write/read many records of three
strings.

I'd like the file to be a .txt file

I think it would be simple to concat the strings with commas seperators
before write, and then use split to separate then after read. Agree?

If that is the approach how do I use streams to read/write?

Thanks

<<snip>>


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.