473,404 Members | 2,178 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,404 software developers and data experts.

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

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
5 2230
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
6
by: Adam H. Peterson | last post by:
I have an application that uses C++ file streams for storing the state of complex objects to be later read in, ideally reconstructing the state of the object as when it was serialized. One issue...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
8
by: bonj | last post by:
hello I hope somebody can help me get my head around this area of 'stream' programming... I know that streams are very fashionable nowadays so hopefully there'll be lots of replies. ;-) ...
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
9
by: Carsten H. Pedersen | last post by:
Hello. Having an issue with double streams... for a lack of a better name. :) I have the following two streams: - FooInputStream, fis, extending InputStream - FooOutputStream, fos, extending...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
0
by: Craig Buchanan | last post by:
when i try to open a file that has been compressed with GZipStream (source from microsoft's 101 Samples for Visual Studio 2005), i get an "invalid archive" message from winzip, zipgenius and xp's...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.