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 5 2259
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(st ring1)
writer.Write(in teger1)
writer.Write(do uble1)
Dim reader As BinaryReader
string1 = reader.ReadStri ng()
integer1 = reader.ReadInt3 2()
double1 = reader.ReadDoub le()
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******** ******@TK2MSFTN GP11.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
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP14.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(st ring1) writer.Write(in teger1) writer.Write(do uble1)
Dim reader As BinaryReader string1 = reader.ReadStri ng() integer1 = reader.ReadInt3 2() double1 = reader.ReadDoub le()
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******** ******@TK2MSFTN GP11.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
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(st ring1)
writer.Write(", "c)
writer.Write(in teger1)
writer.Write(", "c)
writer.Write(do uble1)
writer.WriteLin e()
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.Writ eLine to
format the variables, something like:
Const format As String = "{0}, {1}, {2}"
writer.WriteLin e(format, string1, integer1, double1)
The format parameter is more useful for internationaliz ation & 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******** ******@TK2MSFTN GP15.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:%2******** ********@TK2MSF TNGP14.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>>
It definitely helps
Thanks
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eT******** ******@tk2msftn gp13.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(st ring1) writer.Write(", "c) writer.Write(in teger1) writer.Write(", "c) writer.Write(do uble1) writer.WriteLin e()
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.Writ eLine to format the variables, something like:
Const format As String = "{0}, {1}, {2}" writer.WriteLin e(format, string1, integer1, double1)
The format parameter is more useful for internationaliz ation & 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******** ******@TK2MSFTN GP15.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:%2******** ********@TK2MSF TNGP14.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>>
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******** ******@tk2msftn gp13.phx.gbl... It definitely helps Thanks
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:eT******** ******@tk2msftn gp13.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(st ring1) writer.Write(", "c) writer.Write(in teger1) writer.Write(", "c) writer.Write(do uble1) writer.WriteLin e()
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.Writ eLine to format the variables, something like:
Const format As String = "{0}, {1}, {2}" writer.WriteLin e(format, string1, integer1, double1)
The format parameter is more useful for internationaliz ation & 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******** ******@TK2MSFTN GP15.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:%2******** ********@TK2MSF TNGP14.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>>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 insert in the front. My code is give
below.
Is it possible to do without using XMLDOcument?
Dim masterDoc As String = Request.PhysicalApplicationPath & "PageViews.xml"
Dim writer As XmlTextWriter = Nothing
|
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 I'm encountering,
though, is that when I serialize a double and then read it back in, I've
lost precision on the magnitude of 10^-6. Is there a way to tell a
stream to write a double (or any number format) to whatever precision is
required to...
|
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 possible to change the behavior of
operator << so it will stream numeric values as their actual values
when an ofstream is in binary mode. I did not, however, find any
information on how this can be accomplished. What is involved in
getting this...
|
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. ;-)
Basically I have an operation which the input and output for are streams -
a function which receives a certain 'chunk' of data each time it runs, and
it runs many times until it has received all the data, in a similar way to
a socket.
What the...
|
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, const Bytef *source, uLong
sourceLen);
It is meant to be called by C, but I would rather use it from C#.
So I wrote the following C# program to test it, but it failed to work. The
call to compress doesn't return or throw an exception, it simply...
| |
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 OutputStream
These each have a piped stream as a local variable, so:
- fos has a PipedOutputStream, pos
|
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, with different
file names each time. So i'm writing into the code to ask the user how
many files, and what their names are. From each we'll read in 2 lines,
then do some math using all of those lines. Then do it again on another
set of lines. ...
|
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 compressed folder app.
using DeflateStream resulted in the same error.
am i missing something simple?
thanks,
Craig Buchanan
|
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 build
with these methods, will appear to encrypt and decrypt, but the
resulting decrypted file will be corrupted. I tried encrypting a .bmp
file and then decrypting, the resulting decrypted file under .NET 2.0
is garbage, the .NET 1.1 build works...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |