We help IT Professionals succeed at work.

How to get data from a txt file?

DevelHelper
DevelHelper asked
on
I want to save all test data for different methods in a test.txt file. Do you know how to import those data to an array within the test.vbp which has a menu control to test all those methods? Here is an example:

INSERT_MNL_ACCTS:

123456789,Manual Account Test,111223333,,10,ACB,123 Any Street,Suite 10,,Cleveland,OH,44114,United States,

INSERT_INCL_TRANS:

2,0000320,10182001000001,123456789,09/01/2001

Thanks for your attention!
Comment
Watch Question

Suat OzgurWeb / Application Developer
BRONZE EXPERT

Commented:
you can use Split function in VB6. You should parse the string if you have older version.

Split does that:

'getting string  from text file
myStr="2,0000320,10182001000001,123456789,09/01/2001"
'setting MyArray using "," as delimiter
MyArray=Split(myStr,",")
'now you have UBound.MyArray value in MyArray array.
MyArray(0)="2"
MyArray(1)="0000320"
...
...

Did you need this?

Suat

Author

Commented:
My Question is that how to get string from text file.

Commented:
dim q as long
q=filelen()
Open "c:\test.txt" For Binary As #q\

'if you want to write to the file
Put #q, 1, "testing"

'if you want to read from a specific location
'locations start with 1
'if the string you going to read is 50 character
dim s as string
s=string(50," ")
get #q, 1 , s

'if u donot know the characters lens
dim l as long
dim s as string
l=filelen("c:\test.txt")
s=string(l," ")
get #q,1,s

Commented:
You can also use:

open "test.txt" for binary as #1
mystring = input$(lof(1), #1)
close #1
instead of storing this information as txt file u can store in ini files. This makes ur job more easier while retriving the data rather than in the txt file

Author

Commented:
Shyampaliyath,

Could you please give me an example to show me how to read data from ini? Thanks!

Commented:
Search the VB help files for GetPrivateProfileString and PutPrivateProfileString for the pieces you need to read/write .ini file info.

Author

Commented:
I tried to search for GetPrivateProfileString and PutPrivateProfileString in VB help files. But unfortunately, I got a "Connection Timed Out" message from the window explorer. Could you please copy the code here? Thanks!
Commented:
I don't have the help info handy, but here is some sample stuff:

' Definition (required)
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

' Sample usage
bSuccess = GetPrivateProfileString(strSectionName, strKeyName, "INI_ERROR", strData, lngBufferSize, "xxx.ini")

Where strSectionName = the .ini entry that will appear in "[ ]" and strKeyName = the "variable" name, and strData = space$(255) initially.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.