473,386 Members | 1,609 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,386 software developers and data experts.

Reading Information from a File

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

Sep 30 '07 #1
13 1518
"cmdolcet69" <co************@hotmail.comschrieb
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

Sep 30 '07 #2

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.MalformedLineExceptio n
MsgBox("Line " & ex.Message & _
" is invalid. Skipping")
End Try
End While
End Using

On Sun, 30 Sep 2007 15:58:29 -0700, cmdolcet69 <co************@hotmail.com>
wrote:
>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
Oct 1 '07 #3
Armin,

I have not the idea that CMDolcet looks ever to the answers.

http://groups.google.com/group/micro...rch+this+group

Cor

Oct 1 '07 #4
"Cor Ligthert[MVP]" <no************@planet.nlschrieb
Armin,

I have not the idea that CMDolcet looks ever to the answers.
I am patient (to a certain extent). :)
Armin
Oct 1 '07 #5
On Oct 1, 7:00 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"Cor Ligthert[MVP]" <notmyfirstn...@planet.nlschrieb
Armin,
I have not the idea that CMDolcet looks ever to the answers.

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.

Oct 1 '07 #6
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

Oct 2 '07 #7
OO collection
Oct 2 '07 #8
On Oct 2, 7:14 am, "Cor Ligthert [MVP]" <notmyfirstn...@planet.nl>
wrote:
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

Oct 2 '07 #9
"cmdolcet69" <co************@hotmail.comschrieb
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

Oct 2 '07 #10
On Oct 2, 9:26 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
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

Oct 2 '07 #11
"cmdolcet69" <co************@hotmail.comschrieb
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.
Yeeeaahhh! I love loops! Yeeeahhh.

:-)
Armin
Oct 3 '07 #12
On Oct 3, 6:53 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"cmdolcet69" <colin_dolce...@hotmail.comschrieb
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.

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

Oct 3 '07 #13

"cmdolcet69" <co************@hotmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
On Oct 3, 6:53 am, "Armin Zingler" <az.nos...@freenet.dewrote:
>"cmdolcet69" <colin_dolce...@hotmail.comschrieb
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.

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

Oct 3 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
0
by: EPDJ | last post by:
Well, I have to edit a program which we previously used for reading a header file to be able to read a trailer from a .dat file. The .dat file consists of 5 entries in each record and I am assuming...
1
by: une | last post by:
hey guys, I have this program to do, but I kind of started forst just te read things from the file and then I would go and make the calculation but it is showing some wierd funnky resualts, I dont...
8
namcintosh
by: namcintosh | last post by:
I really need some help. I am trying to read some information from a file in C++. Here is the program that I wrote. (Beware, I am very new to this, so don't freak out if the program seems a...
21
by: Naya | last post by:
Hello, everyone!!! Well, I have a situation here. I am trying to read this data from a file, but the wrong values keep spitting out at me. Here's what I mean: Program: int main() {...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
3
by: itmfl | last post by:
We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
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...

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.