Link to home
Create AccountLog in
Avatar of bradbritton
bradbritton

asked on

How do I read this text file?

I have a text file that is formatted like so:
------------------------------------------
08456018529.biz,3/6/2008,AUC
089world.biz,3/6/2008,AUC
1-2-3easy-buy.biz,3/6/2008,AUC
1-tv.biz,3/6/2008,AUC
1-in-gifts.biz,3/6/2008,AUC
-------------------------------------------
What I need to do is only read the domain name part
08456018529.biz
--- NOT 08456018529.biz,3/6/2008,AUC ---
from this file. I have code below reading the file, but I am unsure how to delimit the file as it has 2 ',' items in the line,

Thanks,

Brad


Dim FILE_NAME As String
        Dim TextLine As String = Nothing
        OFD.ShowDialog()
        FILE_NAME = OFD.FileName
 
        If System.IO.File.Exists(FILE_NAME) = True Then
 
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
 
            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
                Dim n As Integer
                n = Results(TextLine)
                If n > 1 Then
                    MessageBox.Show("Found One!")
                End If
            Loop
 
 
 
        Else
 
            MsgBox("File Does Not Exist")
 
        End If

Open in new window

Avatar of LordWabbit
LordWabbit

You can still delimit on ',' just do this
Dim arrBits() as string = TextLine.Split(","c)
Dim DomainName as string = arrBits(0)
Avatar of Joel Coehoorn
Try this:

If OFD.ShowDialog() = DialogResult.OK AndAlso _
   IO.File.Exists(OFD.FileName) Then
 
    Dim Reader As New System.IO.StreamReader(OFD.FileName)
    While Not Reader.EndOfStream
        If Results(Reader.ReadLine().Split(","c)(0)) > 1 Then
            MsgBox("Found One!")
        End If
    End While
Else
    MsgBox("File Does Not Exist")
End If

Open in new window

Sorry, couldn't help myself, rewrote your code, please don't take offence
        Dim myOfd As New OpenFileDialog
        '-=- if the user doesn't select a file (presses cancel etc.) then exit 
        If (myOfd.ShowDialog() <> Windows.Forms.DialogResult.OK) Then Exit Sub
        '-=- declare the reader outside the try catch so you can make sure it gets closed in the finally 
        Dim myReader As System.IO.StreamReader = Nothing
        Try
            '-=- instantiate the reader
            myReader = New System.IO.StreamReader(myOfd.FileName)
            '-=- read the first line (priming read)
            Dim strLine As String = myReader.ReadLine
            '-=- when the readline returns nothing you have reached the end of the file, so loop till then
            While (strLine Is Nothing = False)
                '-=- split into a string array using , as the delimiter
                Dim arrBits() As String = strLine.Split(","c)
                '-=- not sure what results is supposed to do so...
                If (Results(arrBits(0)) > 1) Then MessageBox.Show("Found one")
                '-=- read the next line (replenish read)
                strLine = myReader.ReadLine
            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Ooops")
        Finally
            '-=- always check for nothing on objects in the finally, an exception thrown in a finally is bubbled up the exception handler stack
            If (myReader Is Nothing) = False Then myReader.Close()
        End Try

Open in new window

There  is no nead for a StreamReader, you can read the lines of the file to an array:
If OFD.ShowDialog() = DialogResult.OK Then
	Dim fileName As String = OFD.FileName
	If System.IO.File.Exists(fileName) Then
		Dim lines As String() = System.IO.ReadAllLines(fileName)
		Foreach line As String in lines
			Dim parts As String() = line.Split(","C)
			If parts.Length > 1
				Dim url As String = parts(0)
				MessageBox.Show(url)
			End If
		Next
	Else
		MessageBox.Show("File Does Not Exist")
	End If
End If

Open in new window

you could greenghost, but if the file is large you are going to run into memory problems.  didn't even know about system.io.readalllines, learnt something new today, tx.
Hi bradbritton;

Here is a solution using the text field parser reader. I have copied GreenGhost code and mad the changes to use the reader.

Fernando
        Dim url As String = String.Empty
        If OFD.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = OFD.FileName
            If System.IO.File.Exists(fileName) Then
                ' Using the Text Field Parser reader to get the URL
                Dim tfp As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName)
                ' Tell the parser what is character to parse on
                tfp.Delimiters = New String() {","}
                ' Tell the parser type of file delimited or fixed width
                tfp.TextFieldType = FileIO.FieldType.Delimited
                ' Trim off any white spaces at the beginning and end of the result string
                tfp.TrimWhiteSpace = True
                While Not tfp.EndOfData
                    ' This method returns and array of string fields and seeming you only 
                    ' want the first field I do not save the array but just get url field
                    ' that is the reason for the (0) at the end to get the first element
                    url = tfp.ReadFields()(0)
                    MessageBox.Show("URL = " & url)
                End While
            Else
                MessageBox.Show("File Does Not Exist")
            End If
        End If

Open in new window

Of course it is good practice to close the reader the reason for repost.
        Dim url As String = String.Empty
        If OFD.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = OFD.FileName
            If System.IO.File.Exists(fileName) Then
                ' Using the Text Field Parser reader to get the URL
                Dim tfp As New Microsoft.VisualBasic.FileIO.TextFieldParser(fileName)
                ' Tell the parser what is character to parse on
                tfp.Delimiters = New String() {","}
                ' Tell the parser type of file delimited or fixed width
                tfp.TextFieldType = FileIO.FieldType.Delimited
                ' Trim off any white spaces at the beginning and end of the result string
                tfp.TrimWhiteSpace = True
                While Not tfp.EndOfData
                    ' This method returns and array of string fields and seeming you only 
                    ' want the first field I do not save the array but just get url field
                    ' that is the reason for the (0) at the end to get the first element
                    url = tfp.ReadFields()(0)
                    MessageBox.Show("URL = " & url)
                End While
                ' Of course it is good practice to close the reader
                tfp.Close()
            Else
                MessageBox.Show("File Does Not Exist")
            End If
        End If

Open in new window

> you could greenghost, but if the file is large you are going to run into memory problems.

Yes, if the file is several hundred megabytes...
> Yes, if the file is several hundred megabytes...

true as well, it would depend on your target minimum specs i suppose, where i work we have several pc's with only 256mb, so i'm always trying to keep my application footprint small
> didn't even know about system.io.readalllines, learnt something new today, tx.

He missed part.  It's IO.File.ReadAllLines().  Still very handy.  I used to use it all the time.  My thought was that it's best to lest the underlying system worry about how to read the file than try to do my own line-by-line buffering.  If I read line by line and disk is ever sitting there idle I'm losing more time than if I let the system read everything at once and then do my processing.

Then I realized that the system was handling buffering anyway, and that by reading a line at a time I was able to start the processing part a little sooner.  Since the processing should go much faster than the disk access, I can be done almost as soon as the file is read.  

I'm not sure which is really better- I should run a benchmark to see if there's even any difference, but to this point it hasn't mattered enough for me to care.
tx jcoehoorn:
i would say as far as speed is concerned reading the entire file into memory and then processing will definintely be faster.  of course it would make a progress bar a bit pointless, and app would prob freeze solid while reading the file making Application.DoEvents and a cancel button also rather pointless.  So I suppose buffer method for GUI's types and ReadAllLines for non GUI service types would be the way to go.
Hehe- if you want to get real fancy, the fastest way would be to read a line at a time in a separate thread, which a notification going to a consumer thread when each line is ready.  But that's just overkill!
but it would be fun to code :-)  
Avatar of bradbritton

ASKER

if built in multi threading into this app would the speed increase alot? Or is it dependent on return query from google?
For small (<100Mb) files you wouldn't notice much difference.  Database and web queries are definitely going to be the bottlenecks.
sounds good, it just takes forever to read the file....I thought that I could cut down time by running multiple threads.
Only if you're reading multiple files.  For one big file it will help, but probably not much.
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
i query google, then save the result to a file.
Ok, then the reading of the file uses about 0.1% of the time. If you use synchronous requests, then something like 95% of the time is spent just waiting for responses. Threading can surely make this a lot more efficient.

You don't have to implement the threading yourself, just use the WebClient.DonwloadStringAsync method to request the pages. When each response comes, the method that you have assigned to the DownLoadStringCompleted property will be called.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I checked the API, but there is no .NET version only PHP
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I feel neglected by that recommendation...
Forced accept.

Computer101
EE Admin