Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read in data from a special text file

Status
Not open for further replies.

Fozzie22

Programmer
Dec 3, 2003
59
AU
Hi everyone,

Its a special text file as it is a cross between a ini file and csv file. I have generated this txt file from a vibration analyser that I made and the format is as follows:

[Parameters]
SampleFreq=5
NoSamples=256

[Inputs]
Ip1=1
Ip2=0

[Data]
1.23,2.11,4.56,5.4,3.1, etc

Ok. I have written some code to read from an INI file that reads the parameters and inputs data. But what I now need is some code that would read in the data fields (csv format) into an array. Does anyone have any suggestions on how I might go about doing this.

Thanks

Fozzie
 
Just as pseudo-code
Code:
Dim nH As Integer
Dim TheLine As String
Dim Fields() As String
nH = FreeFile
Open "C:\FileName" For Input As #nH

Do Until Instr(TheLine, "[Data]", vbTextCompare) > 0
   Input Line #nH, TheLine
Loop

Do Until EOF(nH)
   Input Line #nH, TheLine
   Fields = Split ( TheLine, "," )
   [COLOR=green]' Load the array[/color]
Loop

Assuming thet the [Data] section is the last section in the file.
 
Why write your own code to handle getting examining this file? Windows still supports INI files through the API. So, for example, given the following declaration:
Code:
[blue]Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
[/blue]
the following produces the same result as Golom's code would:
Code:
[blue]Dim strResults As String * 1024
    Dim Fields() As String
    Dim result As Long

    result = GetPrivateProfileSection("Data", strResults, 1024, "C:\demo.ini")
    Fields = Split(Left$(strResults, result - 1), ",")[/blue]
If you been reading ands writing your ini file using all your own code ... well, you may like to look at the following API calls:

WritePrivateProfileInt
WritePrivateProfileSection
WritePrivateProfileString

and

GetPrivateProfileInt
GetPrivateProfileString

along with GetPrivateProfileSection already provided in this example



 
In addition to strongm's excellent advice -- once you've read in the values for your Data section, you can use the Split command to put it into an array.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks golom and strongm. I currently use the WritePrivateProfile... and GetPrivateProfile... so I will modify my existing code and try this. However looking at your advice I cant see it not working perfectly.

Thanks again.
 
Not working properly? Any chance of cluing us in as to why not?
 
Strongm

I think he said "... Can't see it not working perfectly ..." Sorting through the double negatives I think it means it should work perfectly.

Anyway ...

Thanks for stepping in ... I was knee-jerk coding rather than thinking.

The only thing that he (or she?) may need to deal with is parsing multiple lines in the data section. Just doing Split(Left$(strResults, result - 1), ",") will probably return one big honking array with fields from all lines concatenated rather than fields broken out for each line ... or have I missed something ... again?
 
Yep. Sorry. V. tired when I failed to read "can't" properly. Damn those double negatives ...

And yes, if we were to expect multiple lines under [Data] thenn we would suffer the problem you mention - but allt we'd need to do in that situation is simply split on a chr$(0), and then split each of the results as per my example
 
Strongm, just to be clear, are you saying that GetPrivateProfileSection separates multiple lines in a section with a null character? If so, is that behavior typical of returned string values in the Windows API?
 
No, not necessarily typical. But certainly it is the way GetPrivateProfileSection works. And a quick check on MSDN shows it is documented as such - although I'll grant it isn't clear ...
 
So I looked it up.

The data in the buffer pointed to by the lpReturnedString parameter consists of one or more null-terminated strings, followed by a final null character.
Null-terminated string. Duh. So, if there are more characters in the section than there are in the string buffer provided, the characters will be truncated, the return value will be the length of the string, the last two characters of the string will be null, and there won't be any sort of error. Is that correct, especially the error part?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top