d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Probably An Easy Vb Question... > Because I Can Do It In C++ Haha
Add Reply New Topic New Poll
Member
Posts: 3,569
Joined: Dec 27 2006
Gold: 0.69
Apr 8 2013 01:16pm
Can someone give me a little bit of code to load the data from a .txt file into an array in Visual Basic?

Reason why i ask this and doesn't use what i found on google :
Codes i found while searching myself were not always adapted to what i need.
I don't have access to a compiler and i don't know this language.
I'm coding directly into a specific software (that auto-compile and doesn't give me feedback on my errors).

Since i don't know that language... you need what follows... i can't code well into that software...

I know it's shit, but that software is not intented for hard coding new stuff, but to use the functionnalities given by the manufacturer's graphical user interface :)

Thanks in advance!
If you could give me a little help!! :)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 8 2013 04:08pm
question. why are you programing for a language when you dont have that languages compiler.

you can use IO of the open dialog class like so

Code
'Dim OpenAnswerFile As New OpenFileDialog
       Dim strFileName() As String '// String Array.
       Dim tempStr As String = "" '// temp String for result.

       If OpenAnswerFile.ShowDialog = DialogResult.OK Then '// check if OK was pressed.
           strFileName = IO.File.ReadAllLines(OpenAnswerFile.FileName) '// add each line as String Array.
           For Each myLine In strFileName '// loop thru Arrays.
               tempStr &= myLine & vbNewLine '// add Array and new line.
           Next
           MsgBox(tempStr) '// display result.
       End If


or you can do it more like C and open the file as binary

Code
Dim fileNum As Integer
Dim fileLength As Integer
Dim bytes() As Byte
Dim data As Byte
Dim i As Long

fileNum = FreeFile
Open "C:\test.bin" For Binary As fileNum
fileLength = LOF(fileNum)
ReDim bytes(fileLength)
i = 0
Do While Not EOF(fileNum)
 Get fileNum, , data
 bytes(i) = data
 i = i + 1
Loop  
Close fileNum
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Apr 8 2013 07:25pm
filename_string = "C:\z\Words.txt"
Open filename_string For Input As #1

For x_ = 1 To 15
Input #1, input_string
Array(x_) = input_string
Next x_
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll