Solved

Search a text file

Posted on 2004-10-14
14
197 Views
Last Modified: 2010-04-23
I am looking for a solution similar to searching ini files for values. I need to translate ID's to a description ithout connecting to a dtabase (that would be too easy). Instead of using a case statement to cover 250 possible combinations, it seems like an approach similar to ini files in VB6 is appropriate. Is there an efficient way to do this in VB.net? An example of the 2 columns I have to use to search follows. I can export these to a text file with any combination of delimiters, etc that will work.

ID  Description
1   Gas
2   Nitrous Oxide
3   Trinium
4   H2O
5   Silver Nitrate
6   Chromium Sulfate
7   Gold
0
Comment
Question by:JeepGeekin
14 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12308654
open the file via a StreamReader, read it all into a string and then search for your phrase by using
the instr-function or the IndexOf-method provided by the string object.

Dim oStream As New System.IO.FileStream("myfile.txt",IO.FileMode.Open)
Dim oReader As New System.IO.StreamReader(oStream)

Dim strText As String= oReader.ReadAll

Dim strPhrase As String="Find this"
If strText.IndexOf(strPhrase,0)=-1 Then
 MsgBox "I found it!"
Else
 MsgBox "Bad luck, not found"
End If
0
 

Author Comment

by:JeepGeekin
ID: 12308879
Thanks for posting this. It seems like a great way to set a flag, but seems like a work around for returning the following value (the description in my case). I guess try to read from the position returned until the end of the line. Is that how you do this? Do you ever use the API calls to read ini files. Sure there was a sub routine you had to create that was less than easy to follow, but you could feed it the ID & it would return the value each time. You could even group the data & tell it which group to go to. Cool stuff. I wonder why they did away with it in VB.net?

Also, is it good to read in so much data & loop through it like this? I have to do this on 3 or 4 fields all with about 200 - 300 records each on the load event on the form. Sorry. Have to ask.
0
 

Author Comment

by:JeepGeekin
ID: 12309077
I'm open to any other suggestions on this one. Anyone have any? Great code, but not sure it's what I'm looking for. Anyone else have a better idea?
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12310210
what about using a Collection
read the file upon initialization

Dim MyList as New Collection

Private Sub InitializeMyList()
    Dim f As Integer
    Dim strLine As String
    Dim ThisText As String
    Dim SplitLine() As String

    f = FreeFile
    Open "C:\Temp.txt" For Input As #f
    'Don't process the first line
    Line Input #f, strLine

    Do Until EOF(f)
        Line Input #f, strLine
        SplitLine = Split(strLine, Space(1), -1, vbTextCompare)

        MyList.AddItem Splitline(Ubound(splitline),Splitline(1)
     Loop

End Sub

Sub Test ()
'you will have to pass the number you want returned as a string
Msgbox (MyList ("2"))
End Sub

Regards
Corey
0
 

Author Comment

by:JeepGeekin
ID: 12310476
Looks interesting. I had some trouble with Opening the text file the way you are doing it. Also with "Line Input" & "MyList.AddItem Splitline(Ubound(splitline),Splitline(1)". Do I need to import anything for these to work? So far I am importing System.IO.
0
 

Author Comment

by:JeepGeekin
ID: 12310487
Sorry. I have not worked with external files yet. I'm new to VB.net.
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 12311168
Sorry I grabbed an example i had lying around and it was a VB6 example this should get you closer.  It may need a little tweeking.

Corey


include system.collections
include system
Include system.IO

Dim MyList as New Collection

Private Sub InitializeMyList()
            Dim SplitLine as arraylist()
            Dim sr As StreamReader = New StreamReader("C:\Temp.txt")
            Dim line As String
            ' Read and display the lines from the file until the end
            ' of the file is reached.
             line = sr.ReadLine()  
            Do

                line = sr.ReadLine()
                SplitLine = strline.split(space(1),-1)
                 'the numbers may need adjusting I don't remember if it starts at 1 or 0
                MyList.AddItem Splitline(splitline.count) - 1,Splitline(0)

            Loop Until line Is Nothing
            sr.Close()
end sub
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:JeepGeekin
ID: 12311479
OK. I follow this now. I got most of it working (having trouble with the split routine). Until I get this to work I have one question. How does this know to find the ID I am looking for and return the description? I'l look up collections in a bit, but when you are passing it a "2" in your example - does it know to take the "2", find this value in the 1st column (teh ID) and return the value in the second column (the description)? If so, then looks like a great way to handle this. Hope so.
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12311586
I think you got it
A collection is an organized set of objects in this case strings there are 3 ways to parse a collection you can do a For...Each...Next loop or you can get them by their itme number (this isn't suggested because this can change when the collection is edited. or you can select them by their Key.  In the code I took the ID from the file and made it the key of the each object so in your example if you call this line

msgbox (mylist("3"))

it would return a message box containing the word "Trinium"

Corey

What sort of problems are you having with split
you may have to declare strline as a string I would have tested the code but I don't have a .Net compiler on my CPU here.

Dim Strline as String
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12311608
(ps if you have a tab delimited file you will have to change the split to use a tab instead of a space)
0
 
LVL 5

Expert Comment

by:KarcOrigin
ID: 12316135
This is just an idea, why don't you maintain your text file as an ini file data format like -

[ID]
Data

[1]
Gas

[2]
Nitrous Oxide

[3]
Trinium

[4]
H2O

[5]
Silver Nitrate

[6]
Chromium Sulfate

[7]
Gold

?
By doing this your can use rich APIs to read, write the data of your text file that has like ini file data format.
Cheers!
0
 

Author Comment

by:JeepGeekin
ID: 12321233
Thanks for the extra info. It looks like this is what I am looking for.
0
 

Author Comment

by:JeepGeekin
ID: 12322249
Also, since you asked, I don't follow the parameters you are sending for the split function (ie: "space(1),-1").  I couldn't get my program to take these.

Also, About the API call. I read that these are no longer available in .Net or they are being phased out.
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12322667
I don't know what you read but I think it is now a function of the string class.

After a bit of reading it looks like the -1 is probably throwing you this error.  Try using 0 or omiting it completely.

Corey
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now