-
Reading and Writing Data
I'm a VB.Net beginner. I want to read numbers (double) from a text file (many
numbers per record separated by commas), display the valules and perform
some calculations and display the results text boxes as well.
I also want to take those values read and calculated and write them to a
new file.
I've tried using SteamReader/Writer and Parse commands but somehow the formating
is wrong and/or declarations are inconsistent.
I've been searching on the Net, to no avail!
Any ideas?
Thanks
Scoty
-
Re: Reading and Writing Data
Scott,
Perhaps if you post the code you have with a description of what you are
expect compared to what you are getting.
Anthony.
-
Re: Reading and Writing Data
"Anthony Jones" <Ant@yadayadayada.com> wrote:
>Scott,
>
>Perhaps if you post the code you have with a description of what you are
>expect compared to what you are getting.
>
>Anthony.
>
>
Anthony,
Thanks for responding, attached is the code of reference. I get an error
: "Input string was not in a correct format."
The code:
Dim StreamToDisplay As StreamReader
StreamToDisplay = New StreamReader("C:\TEMP\LOXBP.txt")
Dim Strline As String
Dim intline As Integer
Dim data1() As String
Dim data() As String
'Read one line at a time
While StreamToDisplay.Peek <> -1
Strline = StreamToDisplay.ReadLine
intline = intline + 1
ReDim Preserve data(intline)
data(intline - 1) = Strline
End While
'While SReader.Read.Parse(Strline)
' ' txtPin.Text=
'End While
txtPin.Text = StreamToDisplay.Read.Parse(Strline)
txtPsump.Text = StreamToDisplay.Read.Parse(Strline)
txtHpodia.Text = StreamToDisplay.Read.Parse(Strline)
txtLpodia.Text = StreamToDisplay.Read.Parse(Strline)
txtHPOclr.Text = StreamToDisplay.Read.Parse(Strline)
txtLPOclr.Text = StreamToDisplay.Read.Parse(Strline)
txtPistonx.Text = StreamToDisplay.Read.Parse(Strline)
txtSpeed.Text = StreamToDisplay.Read.Parse(Strline)
txtDens.Text = StreamToDisplay.Read.Parse(Strline)
txtK1.Text = StreamToDisplay.Read.Parse(Strline)
txtHpoCd.Text = StreamToDisplay.Read.Parse(Strline)
txtLpoCd.Text = StreamToDisplay.Read.Parse(Strline)
StreamToDisplay.Close()
The LOXBP.txt file contains one record with "double" numbers separated by
commas. What I expect (or want) to see is the numbers displayed in the corresponding
text boxes.
I'm new to this programming and I'm somewhat overwelmed with the vast commands!
Any help is appreciated!
Scott
-
Re: Reading and Writing Data
Scott,
OK first up the initial loop to read lines into an array seems fine
althought the strLine variable seems superflous just assign Readline
directly into the data array. After this loop you will have an array of
strings that represent the set of lines in the file. At this point you
ought to call the StreamReader's close method.
Now what you are trying to do afterward is a little vague because the code
is quite flawed.
Let's just assume that you want to display the fields in the first line of
data in a series of text boxes. Let's also assume that you expect these to
be double precision numbers.
You need to use the split method of the string class in order to seperate
each value in the line. Handily you already have an additional array
dimensioned so you need:
Data1 = Data(0).Split(","c)
See String class Split method documentation for an explanation of how this
works.
Data1 will now have a seperate element for each value in the line.
Since a TextBox displays a string you could do something as simple as
txt.Pin.Text = data1(0)
to show the values.
It seems that the Parse method has you really confused. Parse in these
cases is a shared method. Hence it is not necessary to have an instance of
the destination type in order to call it. Let's just say that before you
assign the value to the text box you want to validate that it does represent
a double precision figure. You could do something like:
txtPin.Text = Double.Parse(data1(0)).ToString
This line attempts to parse data(0) as a double then converts the double
value back into a string. If data1(0) cannot be parsed to a double you get
the formatexception you're getting currently.
HTH,
Anthony.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks