fantasymick
asked on
Simply Reading Off a File
'I have no experience in VB, so this question should be really simple for the people here...
'Bascially, I want to read off a file and dump it into an array of record and read it to a string variable.
'It is a matching program, the input file is this:
'-----------------------
'pic1.jpg
'pic2.jpg
'Which picture has an apple in it?
'Picture1
'-----------------------
'The code is this:
'-----------------------
Private Type rDatabase 'The Record
sPic1 As String * 20
sPic2 As String * 20
sQ As String
sA As String
End Type
Private Sub Form_Load()
Dim aData(1 To 100) As rDatabase 'array of record
Dim iCount As Integer
Dim Index As Integer
Dim sTmp1 As String
Open "G:\VB\Questions.txt" For Output As 1 'open file to read
Index = 0 'array index
Do While (Not EOF(1))
Index = Index + 1
Get #1, , aData.sPic1(Index)
Get #1, , aData.sPic2(Index)
Get #1, , aData.sQ(Index)
Get #1, , aData.sA(Index)
Loop
sTmp = aData.sPic1(1) 'I will assign it to something else, for now, just testing
Close
End Sub
'-----------------------
'When I run it, I get an invalid qualifier error and aData in the loop is highlighted. Also, when I trace, codes within the loop does not run (when aData is removed from the loop). I also get an error in "sTmp = aData.sPic1(1)"
'I realize there must be a lot errors within the codes. Please help a newbie out.
'Bascially, I want to read off a file and dump it into an array of record and read it to a string variable.
'It is a matching program, the input file is this:
'-----------------------
'pic1.jpg
'pic2.jpg
'Which picture has an apple in it?
'Picture1
'-----------------------
'The code is this:
'-----------------------
Private Type rDatabase 'The Record
sPic1 As String * 20
sPic2 As String * 20
sQ As String
sA As String
End Type
Private Sub Form_Load()
Dim aData(1 To 100) As rDatabase 'array of record
Dim iCount As Integer
Dim Index As Integer
Dim sTmp1 As String
Open "G:\VB\Questions.txt" For Output As 1 'open file to read
Index = 0 'array index
Do While (Not EOF(1))
Index = Index + 1
Get #1, , aData.sPic1(Index)
Get #1, , aData.sPic2(Index)
Get #1, , aData.sQ(Index)
Get #1, , aData.sA(Index)
Loop
sTmp = aData.sPic1(1) 'I will assign it to something else, for now, just testing
Close
End Sub
'-----------------------
'When I run it, I get an invalid qualifier error and aData in the loop is highlighted. Also, when I trace, codes within the loop does not run (when aData is removed from the loop). I also get an error in "sTmp = aData.sPic1(1)"
'I realize there must be a lot errors within the codes. Please help a newbie out.
you are using the array incorrectly:
instead of this:
Get #1, , aData.sPic1(Index)
Get #1, , aData.sPic2(Index)
Get #1, , aData.sQ(Index)
Get #1, , aData.sA(Index)
try it this way:
Get #1, , aData(Index).sPic1
Get #1, , aData(Index).sPic2
Get #1, , aData(Index).sQ
Get #1, , aData(Index).sA
AW
instead of this:
Get #1, , aData.sPic1(Index)
Get #1, , aData.sPic2(Index)
Get #1, , aData.sQ(Index)
Get #1, , aData.sA(Index)
try it this way:
Get #1, , aData(Index).sPic1
Get #1, , aData(Index).sPic2
Get #1, , aData(Index).sQ
Get #1, , aData(Index).sA
AW
Use the following method to read a file:
Set References to Microsoft Scripting Runtime in your VB project.
Declare the following variables in Declaration section:
Dim objFSO As FileSystemObject
Dim objTextFile As TextStream
Dim strRead as String
Create the following Sub to Write to a textfile:
Public Sub WriteToFile()
strFileName="C:\My Documents\Testing.txt"
Set objTextFile = objFSO.OpenTextFile(strFil eName, ForReading)
strRead = objTextFile.ReadAll
End Sub
'--- strFileName is the file name where you want to write the text along with the full path of the file.
Set References to Microsoft Scripting Runtime in your VB project.
Declare the following variables in Declaration section:
Dim objFSO As FileSystemObject
Dim objTextFile As TextStream
Dim strRead as String
Create the following Sub to Write to a textfile:
Public Sub WriteToFile()
strFileName="C:\My Documents\Testing.txt"
Set objTextFile = objFSO.OpenTextFile(strFil
strRead = objTextFile.ReadAll
End Sub
'--- strFileName is the file name where you want to write the text along with the full path of the file.
Hi,
Varfile = FreeFile
FilePath = App.Path & "\Nambi.dat"
'FilePath = "C:\FrunLog.txt"
Open FilePath For Input As #Varfile ' Open File
Length = LOF(Varfile) ' Length of File
Text1 = Input(Length, Varfile) 'Get File Details and Store them into Text1
Noe You Have the Contents in Text1.
Dim Arr
Arr=Split(Text1," ")
Then it will Split the Every Word From the Text1 and Store Eac Wod into the Array Called Arr.
UBound(Arr) is used to findout the No of Elements.
for i=0 to ubound(Arr)
msgbox arr(i) 'to display Every Word
Next
Regards,
Nambi
Varfile = FreeFile
FilePath = App.Path & "\Nambi.dat"
'FilePath = "C:\FrunLog.txt"
Open FilePath For Input As #Varfile ' Open File
Length = LOF(Varfile) ' Length of File
Text1 = Input(Length, Varfile) 'Get File Details and Store them into Text1
Noe You Have the Contents in Text1.
Dim Arr
Arr=Split(Text1," ")
Then it will Split the Every Word From the Text1 and Store Eac Wod into the Array Called Arr.
UBound(Arr) is used to findout the No of Elements.
for i=0 to ubound(Arr)
msgbox arr(i) 'to display Every Word
Next
Regards,
Nambi
Hi,
For reading Contents From File U Must open it on Input Mode. Not in OutPut Mode.
Split Fn is used to split the Content according to Your Delimiter and store tem into Array.
So I Hope Code avail above Will be helpful.
Regards,
Nambi
For reading Contents From File U Must open it on Input Mode. Not in OutPut Mode.
Split Fn is used to split the Content according to Your Delimiter and store tem into Array.
So I Hope Code avail above Will be helpful.
Regards,
Nambi
in aqddition to my earlier comment, you should also change this:
sTmp = aData.sPic1(1)
to:
sTmp = aData(1).sPic1
since aData is the ARRAY, you need to specify the index on the ARRAY, in each case:
aData(Index).------
not
aData.-----(Index)
AW
sTmp = aData.sPic1(1)
to:
sTmp = aData(1).sPic1
since aData is the ARRAY, you need to specify the index on the ARRAY, in each case:
aData(Index).------
not
aData.-----(Index)
AW
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open "G:\VB\Questions.txt" For Input As 1
You are reading the file INto your program not printing it OUT from your program.
mlmcc