Reading Information from a File

C

cmdolcet69

I have this code below that will open a created file and read the file
to the end. However if my file has values 200,300,400 seperated by
commas how can i read in each seperate value and place it into an
arraylist? I need to do some sort of commas split to just read the
values and not the commas.

Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String

Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
oRead.Close()

Catch ex As Exception
'Error Handling Code.
End Try
 
A

Armin Zingler

cmdolcet69 said:
I have this code below that will open a created file and read the
file to the end. However if my file has values 200,300,400 seperated
by commas how can i read in each seperate value and place it into an
arraylist? I need to do some sort of commas split to just read the
values and not the commas.

Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String

Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
oRead.Close()

Catch ex As Exception
'Error Handling Code.
End Try


Have a look at the methods of the String object. It also provides a Split
function. You can add several items to an arraylist at once using it's
AddRange method.


Armin
 
J

Jay Pondy

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {vbTab}
Dim currentRow As String()
'Loop through all of the fields in the file.
'If any lines are corrupt, report an error and continue parsing.
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
' Include code here to handle the row.
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
" is invalid. Skipping")
End Try
End While
End Using
 
C

cmdolcet69

I am patient (to a certain extent). :)

Armin

Sorry Cor i missed that one in my threads.......my main question would
be how can i parse out the commas when readings a file?

sorry again and will do my best to keep up on things.
 
C

Cor Ligthert[MVP]

Just use OleDB and datatables to get CSV files. It is so easy. Ten lines of
code and you have the file in a nice OP colletion. The Datatable in the
DataSet.

http://www.vb-tips.com/CSVDataSet.aspx

I never tried it, however I assume that you can fill a datatable direct as
well.

Cor
 
C

cmdolcet69

OO collection

Cor, that not exactly the thing i wanna do however i did take a look
at it and can maybe use it on another project, however i believe the
code below that i wrote will work but i have a problem with my string
array that i created. Below i parse out a file (sample.txt) i then
place the parsed data into an array of strings.

Public FileArray As New ArrayList
Public FileData As String()
Public EntireFile As String

Private Sub btnUploadFile_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnUploadFile.Click
Dim oFile As File
Dim oRead As System.IO.StreamReader
Dim EntireFile As String
'Dim FileData As String()
' Dim FileArray As New ArrayList
Try
oRead = oFile.OpenText("C:\sample.txt")
EntireFile = oRead.ReadToEnd()
FileData = Split(EntireFile, ",")
'FileArray.Add(FileData)
oRead.Close()
Catch ex As Exception
'Error Handling Code.
End Try
End Sub


After this code is executed i pass my FileArray into another event
handlers which is. This isn;t the complete code however it will break
at the line bitvalue= bitconverter.... I ran a similar program were i
would add my arralist.add(value) into the bitvalue equation that work
but when i run the code below i get the following.
"Overload resolution fail because no accessbile 'GetByte' can be
called without narrowing conversion. Public shared function GetByte
(value as Double) as Byte argument matching parameter value narrows
fro string to double".

Dim i As Integer = 0
Dim j As Integer = 0
ScaleLocation = 0
Try

For i = 0 To 1001
'convert to bytes
bitvalue = BitConverter.GetBytes(FileData(i))
bitlocation = BitConverter.GetBytes(ScaleLocation)
Next
Catch ex As Exception
' Warn the user.
MessageBox.Show("Upload Command is incorrect")
Finally
End Try
End Sub
 
A

Armin Zingler

cmdolcet69 said:
After this code is executed i pass my FileArray into another event
handlers which is.

Which is....? What does "pass [...] into another event handler" mean?
This isn;t the complete code however it will
break at the line bitvalue= bitconverter.... I ran a similar program
were i would add my arralist.add(value) into the bitvalue equation
that work but when i run the code below i get the following.
"Overload resolution fail because no accessbile 'GetByte' can be
called without narrowing conversion. Public shared function GetByte
(value as Double) as Byte argument matching parameter value narrows
fro string to double".

Dim i As Integer = 0
Dim j As Integer = 0
ScaleLocation = 0
Try

For i = 0 To 1001
'convert to bytes
bitvalue = BitConverter.GetBytes(FileData(i))


You try to pass a String, but if you look at the list of available
overloads, it is not possible to pass a string. What do you expect from the
function? If you expect BitConverter to convert each char in the string,
then write a loop.


Armin
 
C

cmdolcet69

cmdolcet69 said:
After this code is executed i pass my FileArray into another event
handlers which is.

Which is....? What does "pass [...] into another event handler" mean?




This isn;t the complete code however it will
break at the line bitvalue= bitconverter.... I ran a similar program
were i would add my arralist.add(value) into the bitvalue equation
that work but when i run the code below i get the following.
"Overload resolution fail because no accessbile 'GetByte' can be
called without narrowing conversion. Public shared function GetByte
(value as Double) as Byte argument matching parameter value narrows
fro string to double".
Dim i As Integer = 0
Dim j As Integer = 0
ScaleLocation = 0
Try
For i = 0 To 1001
'convert to bytes
bitvalue = BitConverter.GetBytes(FileData(i))

You try to pass a String, but if you look at the list of available
overloads, it is not possible to pass a string. What do you expect from the
function? If you expect BitConverter to convert each char in the string,
then write a loop.

Armin- Hide quoted text -

- Show quoted text -

Armin, i wnat to do the same thing like i did with the code below: I
added value to an arraylist and then looped through those value.
However with the code in the previous post all im doing is reading and
parsing from a file. How can i get the previous post to work like my
code in this post. Does that make sense?

Public bitvalue As Byte()
Public ArrList As New ArrayList

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim i As Integer = 0
Dim completelist As New ArrayList
ArrList.Add(3000)
ArrList.Add(3003)
ArrList.Add(3006)
ArrList.Add(3009)
ArrList.Add(3012)
ArrList.Add(3015)
For i = 0 To 5
bitvalue = BitConverter.GetBytes(ArrList(i))
Next
End Sub
 
A

Armin Zingler

cmdolcet69 said:
You try to pass a String, but if you look at the list of available
overloads, it is not possible to pass a string. What do you expect
from the function? If you expect BitConverter to convert each char
in the string, then write a loop.

Armin- Hide quoted text -

- Show quoted text -

Armin, i wnat to do the same thing like i did with the code below:
[...]


Can we please first handle the code above before mixing it with the other
code? Thx.

So, again: Why does my suggestion not help? Why not write a loop in order to
handle each char in the string?


Armin
 
C

cmdolcet69

Armin, i wnat to do the same thing like i did with the code below:
[...]

Can we please first handle the code above before mixing it with the other
code? Thx.

So, again: Why does my suggestion not help? Why not write a loop in order to
handle each char in the string?

Armin- Hide quoted text -

- Show quoted text -

Armin sorry i will focus onthe previous code. what i meant by "pass
[...] into another event handler" was that i pass my arraylist into
another event handler( button)
anyways can u give me a small example of where you would suggest me to
put the for loop in?
 
A

Armin Zingler

cmdolcet69 said:
Dim i As Integer = 0
Dim j As Integer = 0
ScaleLocation = 0
Try
For i = 0 To 1001
'convert to bytes
bitvalue = BitConverter.GetBytes(FileData(i))
You try to pass a String, but if you look at the list of
available overloads, it is not possible to pass a string. What
do you expect from the function? If you expect BitConverter to
convert each char in the string, then write a loop.
Armin- Hide quoted text -
- Show quoted text -
Armin, i wnat to do the same thing like i did with the code
below: [...]

Can we please first handle the code above before mixing it with
the other code? Thx.

So, again: Why does my suggestion not help? Why not write a loop
in order to handle each char in the string?

Armin- Hide quoted text -

- Show quoted text -

Armin sorry i will focus onthe previous code. what i meant by "pass
[...] into another event handler" was that i pass my arraylist into
another event handler( button)

I don't understand. The signature of the event handler is determined by the
class. You can not pass extra data into an event handler. (signature = count
and type of parameters) But that's not the important point.
anyways can u give me a small example of where you would suggest me
to put the for loop in?

I don't know what you want to do. You have strings in an Array. What do you
want to do with each string? What do you want to do with each char in the
string? If you want to process each character in the string, you can write:

For i = 0 To 1001

for each c as char in FileData(i)
dim bytes as byte()
bytes = bitconverter.getbytes(c)
next
Next

In the inner loop, each character is converted into an array of bytes.
Though, I don't know what you wanna do with this array.


Armin
 
C

cmdolcet69

Dim i As Integer = 0
Dim j As Integer = 0
ScaleLocation = 0
Try
For i = 0 To 1001
'convert to bytes
bitvalue = BitConverter.GetBytes(FileData(i))
You try to pass a String, but if you look at the list of
available overloads, it is not possible to pass a string. What
do you expect from the function? If you expect BitConverter to
convert each char in the string, then write a loop.
Armin- Hide quoted text -
- Show quoted text -
Armin, i wnat to do the same thing like i did with the code
below: [...]
Can we please first handle the code above before mixing it with
the other code? Thx.
So, again: Why does my suggestion not help? Why not write a loop
in order to handle each char in the string?
Armin- Hide quoted text -
- Show quoted text -
Armin sorry i will focus onthe previous code. what i meant by "pass
[...] into another event handler" was that i pass my arraylist into
another event handler( button)

I don't understand. The signature of the event handler is determined by the
class. You can not pass extra data into an event handler. (signature = count
and type of parameters) But that's not the important point.
anyways can u give me a small example of where you would suggest me
to put the for loop in?

I don't know what you want to do. You have strings in an Array. What do you
want to do with each string? What do you want to do with each char in the
string? If you want to process each character in the string, you can write:

For i = 0 To 1001

for each c as char in FileData(i)
dim bytes as byte()
bytes = bitconverter.getbytes(c)
next
Next

In the inner loop, each character is converted into an array of bytes.
Though, I don't know what you wanna do with this array.

Armin- Hide quoted text -

- Show quoted text -

Armin I was able to get this conversion to work. I thank you again and
appreiciate all your help. You post help me think outside the box and
further.
Thanks again i was able to make my own loop and convert the string.

THanks
 
A

Armin Zingler

cmdolcet69 said:
Armin I was able to get this conversion to work. I thank you again
and appreiciate all your help. You post help me think outside the
box and further.
Thanks again i was able to make my own loop and convert the string.

Yeeeaahhh! I love loops! Yeeeahhh.

:)


Armin
 
C

cmdolcet69

Yeeeaahhh! I love loops! Yeeeahhh.

:)

Armin

Oh boy i love loops too, Armin how can i exit my for loop we where
just talking about if my user hits a button on the screen. I want to
make sure that if he see a problem on the screen he can hit an exit
button and exit oout of the loop, rather than waiting the 4 minutes or
so till it completes.
Thanks
 
L

Lloyd Sheen

cmdolcet69 said:
Oh boy i love loops too, Armin how can i exit my for loop we where
just talking about if my user hits a button on the screen. I want to
make sure that if he see a problem on the screen he can hit an exit
button and exit oout of the loop, rather than waiting the 4 minutes or
so till it completes.
Thanks

Check out the DoEvents method. Place this in your loop (try not to do it
everytime as this may slow the process down considerably), and then the
event for the button click will be called when the button is clicked. Set a
variable within the scope of both your routine that has the loop processing
and the event handler so that it can indicate when to stop.

Psuedo code:


Loop

Do things

DoEvents

If stop_variable = true then
exit loop
end if
End Loop


Hope this helps
Lloyd Sheen
 
A

Armin Zingler

cmdolcet69 said:
Oh boy i love loops too, Armin how can i exit my for loop we where
just talking about if my user hits a button on the screen. I want to
make sure that if he see a problem on the screen he can hit an exit
button and exit oout of the loop, rather than waiting the 4 minutes
or so till it completes.
Thanks


This requires multiple threads. The first is the (default) one that you
already have. The second one would have to be created in order to do the job
that we were talking about. Meanwhile, the Form in the first thread stays
responsive and a button can be used to cancel the 2nd thread. As
multithreading is an advanced technology if you want to make it bulletproof,
you should really think twice whether you want to tackle it.

http://msdn2.microsoft.com/en-us/library/3e8s7xdd.aspx
http://msdn2.microsoft.com/en-us/library/eed6swsx(VS.80).aspx


Armin
 
C

cmdolcet69

This requires multiple threads. The first is the (default) one that you
already have. The second one would have to be created in order to do the job
that we were talking about. Meanwhile, the Form in the first thread stays
responsive and a button can be used to cancel the 2nd thread. As
multithreading is an advanced technology if you want to make it bulletproof,
you should really think twice whether you want to tackle it.

http://msdn2.microsoft.com/en-us/li...rosoft.com/en-us/library/eed6swsx(VS.80).aspx

Armin

what would be the best way to approach this using existing tools. I
want to cancel the upload data event if somthing occurs, I would like
to have a button if possible so that my user can react to this. I
guess i could set a flag based on a condition that it would enter and
then i could call my exit for loop command.....does that make any
sense?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top