Jump to content

Read from file help needed

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
deloreandude

deloreandude

    Newbie

  • Members
  • Pip
  • 8 posts
Hi everyone

Im trying to do something in VB.net and failing.

I want to read integers as they are below from a file and put them into an array of integer.

2,4,3,1,2,4,5,6,4,3,2,4,2
2,4,7,5,3,8,2,4,3,2,4,2,0

Each line has the same number of numbers. and each line will go to a seperate array

How can i read this data and place it into an array of type integer.
The usuall way using streamreader gets me data type missmatch errors.

Thanks in advance

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
That's because of the commas. You're better off replacing the commas with spaces; try that first, and if it doesn't work, let us know.

#3
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You shouldn't need that. Just read each line, and use the Split(",") string function to split it automatically into an array!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#4
deloreandude

deloreandude

    Newbie

  • Members
  • Pip
  • 8 posts
Ok im still having problems with this..

the file needs to contain integers but the format can be changed
such as 1,2,3,1,5,2,4 or with spaces instead...

i need to ultimately get these bumbers into a 2D array... however for now into a 1D as integers would be fine.

they must be as integers,

any help would be appreciated.. thanks

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Dim data As String =  System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(data.Split(",").Length) As Integer = data.Split(",")
Untested, but does it work?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#6
deloreandude

deloreandude

    Newbie

  • Members
  • Pip
  • 8 posts
Dim data As String = System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(10) As Integer
thearray = data.Split(",")

had to change the code because of array initialize error.. and now it says

value 1 dimentional array ... cannot be convert..because string is not derived from integer

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Aha! Replace the last line with:
thearray = CInt(data.Split(","))

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
deloreandude

deloreandude

    Newbie

  • Members
  • Pip
  • 8 posts
just an error saying

value of type 1 d array of string cannot be converted to an integer

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
OK then.

Dim data As String = System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(10) As String
thearray = data.Split(",")

You have an array of string values.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#10
deloreandude

deloreandude

    Newbie

  • Members
  • Pip
  • 8 posts
ok so how do i change that to an array of integer values?

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
Just go through each item and use CInt() on it, and store it in a second integer array.

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yep. Use a for loop or a foreach loop.

Dim data As String = System.IO.File.ReadAllText("C:\thefile.txt")
Dim thearray(10) As String
thearray = data.Split(",")

Dim intarray(thearray.Length) As Integer

For (index As Integer) = 0 To thearray.Length - 1
 intarray(index) = CInt(thearray(index))
Next index

Untested, but does it work?

Edited by Xav, 27 July 2008 - 05:17 AM.

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums