Reading integers, floating point numbers, etc. on the same line froma text file

  • Thread starter Thread starter C
  • Start date Start date
C

C

Older programs produce output files in their own ways, and they have
to be read by new programs, so I am trying to figure out how best to
read some of the older files.

Suppose the first line in the file is
8 3 0.3452348E-04 "left side"

The first two are always integers, followed by a single precision
floating point number, followed by a string (in quotes if there are
blanks inside, and without quotes if it is a single word). In VB6, I
could write Input #1, N, M, x1, varname

Then the next three lines contain an 8 by 3 matrix of floating point
numbers. The number of blank spaces in between the fields vary since
they have been written by Print statements in VB6.

I wonder if there is a good way of reading these files in VB.net.

The inefficient way I can think of is to read blanks, then collect the
string one character at a time, convert to expected type and continue
that way. If you hit a quote, then stop looking for blanks until quote
ends. But this looks so inefficient and will require 40 lines of code
to do what we earlier did in one simple statement.

I have started to learn VB.net just last week, so I will have more
stupid questions.
 
You can probably trim down your code using the "Split" function and
splitting up all the 'words' with spaces.

When you go through the list it creates however - you are correct, you will
have to see if the word contains a quote and then search from there on, for
the other quote to re-create the sentance.

Using the split program would get rid of you "searching each character by
spaces" however.
But it would trim your code down to about 5-10 lines ( because of the quote
issue)

Example:
http://dotnetperls.com/split-examples-vbnet

-Curious to know if anyone has other ideas?

Miro
 
Older programs produce output files in their own ways, and they have
to be read by new programs, so I am trying to figure out how best to
read some of the older files.

I've seen your thread on the VB.CLASSIC group... As much as I don't like
these functions, if these were written using the VB.CLASSIC Write statement,
then you are in luck, since the Input statement is still supported in VB.NET.
It has become the input function. Take a look at the FreeFile function,
FileOpen, FileClose, Input, Write functions in the documentation - I think
they will get you were you want to go (I believe there are even some code
examples in the docs.)
 
Here is some very rough code that will parse a file and extract anything
that looks like a number. Use it on a form with two listboxes and you can
compare the text as found on the left with the numbers it identified on the
right.

The rule is, if the character following a space is a digit, or a + or -
followed by a digit, then interpret the characters up to the next space as a
number. This rule automatically ignores trailing non-digits, but includes
numbers in e format.

For dates and times it will take the digits up to the first separator only -
if these can appear in your file then they will have to handled as
exceptions.

Once you have your list of numbers you will need to do counting to work out
which is which, but it seems you can simply group them as 9 sets of three.
When you know which is which you can cast them to the correct type.

Dim sr As System.IO.StreamReader = System.IO.File.OpenText("<your filename
goes here>")
Dim s As String = sr.ReadToEnd
Dim sa As String() = s.Split(" ")
Dim n As Double
For i As Integer = 0 To sa.Length - 1
Dim atom As String = Trim(sa(i))
If atom <> "" Then
ListBox1.Items.Add(atom)
If atom.Length = 1 Then
Dim t0 = atom.Substring(0, 1)
If (t0 >= "0" And t0 <= "9") Then
'is a number
n = Val(atom)
ListBox2.Items.Add(n.ToString)
Else
ListBox2.Items.Add("NaN")
End If
Else
Dim t0 = atom.Substring(0, 1)
Dim t1 = atom.Substring(1, 1)
If (t0 >= "0" And t0 <= "9") Or _
((t0 = "+" Or t0 = "-") And (t1 >= "0" And t1 <= "9")) Then
'is a number
n = Val(atom)
ListBox2.Items.Add(n.ToString)
Else
ListBox2.Items.Add("NaN")
End If
End If
End If
Next i
sr.Close()
 
I've seen your thread on the VB.CLASSIC group...  As much as I don't like
these functions, if these were written using the VB.CLASSIC Write statement,
then you are in luck, since the Input statement is still supported in VB.NET.
It has become the input function.  Take a look at the FreeFile function,
FileOpen, FileClose, Input, Write functions in the documentation - I think
they will get you were you want to go (I believe there are even some code
examples in the docs.)

Thanks for your help. Apparently, the Input function is not VB.net
although it is usable at present. VB Express converted my VB6 code's
Input statements in that way, and they run fine, but I have also been
warned that it is not VB.net and will probably disappear some day not
very far in future. I thought there should be a similar VB.net way to
read up a series of integers and floating point numbers.

Looks like there isn't, so the best I can do is to write a little
function ReadNextField which will read up the first blanks, if any,
followed by non-blank characters and then convert that string to
Int32, Short, Double, etc. Since I am not familiar with VB.net, it
will be quite an exercise to do this little task.

There is a good reason why the numbers were written with Print the way
they have been written for a decade. Some of the numbers form
matrices, which I often want to read. Sometimes I want to manually
combine two such result files into a single file, where I will have to
combine the matrices also by appending the rows, and changing the
values of the matrix sizes, and doing other small things like
appending new names for the second matrix, etc. IF this is in a comma
separated format, its readability will suffer. Blank separated files
are good because they are also easy to read with VB6. I did not expect
MS to mess up this part so badly. Of course, I can manage with the
Input (1, M) for now.
 
Thanks for your help. Apparently, the Input function is not VB.net
although it is usable at present. VB Express converted my VB6 code's
Input statements in that way, and they run fine, but I have also been
warned that it is not VB.net and will probably disappear some day not
very far in future. I thought there should be a similar VB.net way to
read up a series of integers and floating point numbers.

Ahhh... I wish people would get that one straight Input is VB.NET and is
not going to disapper. Those functions are part of the
Microsoft.VisualBasic namespace - that makes them core functions of the
language. The functions that might disappear, are those in
Microsoft.VisualBasic.Compatability.

While the Input function is not recommended for use in applications that don't
have to deal with interop - they are not going to be removed.
 
Thanks for your help. Apparently, the Input function is not VB.net
although it is usable at present. VB Express converted my VB6 code's
Input statements in that way, and they run fine, but I have also been
warned that it is not VB.net and will probably disappear some day not
very far in future. I thought there should be a similar VB.net way to
read up a series of integers and floating point numbers.

The Input function is unlikely to be going away. I'm not sure whether
it is in the compatibility namespace or not, but that's irrelevant --
it's in use, so they are unlikely to replace it without providing
equivalent functionality. And really, why should they? It's written
in .net, it almost certainly doesn't do anything particularly strange
or fragile, why remove?
 
Ahhh...  I wish people would get that one straight Input is VB.NET and is
not going to disapper.  Those functions are part of the
Microsoft.VisualBasic namespace - that makes them core functions of the
language.  The functions that might disappear, are those in
Microsoft.VisualBasic.Compatability.

Thanks for this clarification. This is very good news.

As I mentioned, I am a beginner, so I appreciate this.
While the Input function is not recommended for use in applications that don't
have to deal with interop - they are not going to be removed.

What is interop? I don't think I will need to communicate with other
programs or across the network.
 
The Input function is unlikely to be going away.  I'm not sure whether
it is in the compatibility namespace or not, but that's irrelevant --
it's in use, so they are unlikely to replace it without providing
equivalent functionality.  And really, why should they?  It's written
in .net, it almost certainly doesn't do anything particularly strange
or fragile, why remove?


Sounds good.
 
What is interop? I don't think I will need to communicate with other
programs or across the network.

In this particular case, it means dealing with legacy file formats and and
applications.
 
Thanks for this clarification. This is very good news.

As I mentioned, I am a beginner, so I appreciate this.

The Input function isn't really that complicated, depending upon your
usage, you might consider using a TextReader in it's place.
 
Back
Top