dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] Cannot read a file line-by-line...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Resolved [RESOLVED] Cannot read a file line-by-line...

    I've opened a file using the following:

    Code:
    Open "C:\Lab\temp.htm" For Input As 1
    Dim U As String
            While EOF(1) = 0
            Line Input #1, U
            Msgbox U
    Wend
    Close #1
    ...but the message box only shows about seven lines, even though the file is quite a bit more than that. It seems as though it's not breaking down the "temp.htm" file into separate lines... kind of like it is all one line and the code I use is looking for carriage returns or something. I need to be able to read the file line-by-line, which will allow me to do what I need to with each line.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    Re: Cannot read a file line-by-line...

    Line Input will read one line from a file, in a loop it will read all lines one at a time.
    If you are not seeing this behavior then it would seem that the file does not have proper line breaks in it.

    If you open the file in notepad and turn off word wrapping do you see the same result as from your program?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Cannot read a file line-by-line...

    When I opened it in Notepad and turned off word wrap, as you suggested, it does lessen the amount of lines, but the file is so large that I think Notepad itself has a limit to the amount of characters that will fit on one line. I suppose I could go in and insert a carriage return after a certain character, but I'm not sure how I would go about doing so.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,378

    Re: Cannot read a file line-by-line...

    Geezes... just how wide is the file? yes notepad has a limit, I think it's somewhere around 2k... I know I've reached it from time to time. At any rate, also take a look at Notepad++ (N++, NPP) it's got wider widths and more goodies. Keep in mind that Notepad is just a basic simple text editor.

    That said, if you got 7 lines, then 7 lines are what's in the file using CRLF as the line terminator... now MsgBox WILL have a limit... there's only so much it can display... and honestly, there's only so much a sane person is going to want to read in a messagebox.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Cannot read a file line-by-line...

    It seems your file may have long text lines or that it is a file with Unix style line delimiter instead if windows style line delimiters (vbCrLf),or both.

    Another thing is that, as mentionned earlier, textbox control does not have the capability to load large files (32k the limit).

    Is your goal to load the text in a control or to have access to individual lines?

    Either way, there as simple solutions.

    1- Use a control that can handle both size and Unix style line delimiter.

    Code:
    RichTextBox1.filename = yourfilename$
    allLines = Split(RichTextBox1.text, vbCrLf) 'only needed if you need access to individual lines
    2- While the above works well with a minimal amount of code, the split function can be slow on very large strings. A faster option (albeit slower than VB native line input, which does insist on lines separated with vbCrLf) is to use the file script object (FSO), which will properly recognize vbLf as the line delimiter.

    Code:
    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Set ts = fso.OpenTextFile(yourfilename$)
    Do While Not ts.AtEndOfStream
        oneline$ = ts.ReadLine
        ' do whatever you want with each line here
    Loop
    ts.Close
    There are other options but the above two should have you covered.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    7

    Re: Cannot read a file line-by-line...

    try this

    Dim fileNum
    fileNum = FreeFile
    Open "F:\yourfolder\yourfile.txt" For Input As #fileNum
    Do Until EOF(fileNum)
    Line Input #fileNum, temp
    Msgbox temp
    Loop
    Close #fileNum

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,010

    Re: Cannot read a file line-by-line...

    numus.....best not to use a MsgBox in a loop like that....if that is a huge file (many lines) as I suspect, OP would have to click the OK button on the MsgBox MANY MANY MANY times.....Your solution is a simple one for smaller files, but still, don't use msgbox...use Debug.Print instead if you are just testing, otherwise do whatever you want with each line...but no messagebox!

    OP-same applies to you. avoid using a MSgBox in a file loop unless you KNOW it is a small file (then, I would STILL use debug.print in testing instead.

    OP-can you attach the file to this thread? Only do so if it does not contain PII (personal identifiable information) or other data that should remain 'secret'.

  8. #8
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    644

    Re: Cannot read a file line-by-line...

    Welcome, I suspect, to the thorny world of files created for operating systems other than Windows.

    Line Input assumes that every "line" will end in the characters vbCr followed by vbLf, because that's what Windows uses. Files created on Unix machines or Mac's are different; they use just vbLf or vbCr, respectively. Nothing like consistency, eh?.

    HTML, to be different again, doesn't care a fig about line breaks at all; it's constructed from all those angled braces ("<" and ">") spread all over the place.

    Open your file in Notepad and you'll probably see much the same thing (which is why no serious Developer uses Notepad).

    You need to convert the line breaks into Windows ones and then your code will work. However ...

    Do you really need to read the file line by line? If the file is small[-ish], you might be better off reading the whole lot into memory and then breaking it into individual lines to work with. Line-by-line tends to be slower (but less memory intensive); it's [always] a trade -off.

    If you really have to stick with line-by-line, then try something like this:

    Code:
    ' Don't hard code file numbers.  
    '   You never know if some other part of your program might decide to do some file stuff.  
    '   FreeFile() always get you a unique file number (unless you use all 255 of them, of course). 
    Dim hFile as Integer 
    
    Dim U As String
    
    hFile = FreeFile() 
    Open "C:\Lab\temp.htm" For Input As hFile
    
    ' Do .. Loop gives you more flexibility than While .. Wend 
    Do While ( Not( EOF( hFile ) ) ) 
       Line Input #hFile, U
       Call MsgBox( U ) ' "Call"?  Just a personal habit, because I do .Net stuff as well. 
    Loop
    Close #hFile
    OK, it'll work, but it's bad practice, IMHO.
    You're holding a file open while the user is doing something interactive. They might leave that MsgBox and wander off to lunch. If someone else comes along and wants to do something with that file, they can't, because the first use has the file "locked" open.
    I'd suggest it's better to get the file handling out of the way quickly, which bring me back to doing it all in one go:

    Code:
    Dim V As String 
    
    hFile = FreeFile() 
    Open "C:\Lab\temp.htm" For Input As hFile
    
    ' Read the whole file into U
    U = Input( LOF( hFile ), #hFile )
    Close #hFile ' The file's now available for someone else to use.  
    
    For Each V in Split( U, vbCrLf ) ' or vbLf or vbCr; you need to find out which
       Call MsgBox( V ) 
    Next
    Regards, Phill W.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Cannot read a file line-by-line...

    Okay... I am not attempting to put anything in a TextBox. Not sure where anyone got that. The file is actually the source code of a web page with the extension .htm. I am attempting to retrieve the links from that source code file that match a certain criteria (ones that don't have "google" in them, ones that are longer than 40 characters, etc.). Seeing as Google's search mashes their source code almost onto a single line, it is difficult to sort through each line to check if it has a link in it (a href). And that's what I need. So, up front, I need to separate the file into lines, though ultimately I need to rip the links from the page. Ripping the links is easy. Breaking down the file into lines would be even better.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Cannot read a file line-by-line...

    Phil: Your final code piece gives me the error "Compile error: For Each control variable mus be Variant or Object" on the line

    Code:
    For Each V In Split(U, vbCrLf)

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Cannot read a file line-by-line...

    Also, I use a msgbox just for me... not for the final design. I don't care to use the debug window. Just my preference.

  12. #12
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    644

    Re: Cannot read a file line-by-line...

    Quite right. That's the danger of "Air-Coding".

    This should work a little better:
    Code:
    Dim V as Variant ' String 
    For Each V In Split(U, vbCrLf) 
       . . .
    Regards, Phill W.

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Cannot read a file line-by-line...

    Quote Originally Posted by Conroy Vanderbluff View Post
    I am attempting to retrieve the links from that source code file that match a certain criteria (ones that don't have "google" in them, ones that are longer than 40 characters, etc.).
    That could be a very big task doing it using Line Input method or even using Binary input file and even then you may not get all the links not to mention all the parsing trouble you would have to go through.

    A better way to extract links is to use the WebBrowser and set up your code to extract out all the links into a Listbox or some other string array. Then extract from that the links you want


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  14. #14
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    644

    Re: Cannot read a file line-by-line...

    Ripping the links is easy. Breaking down the file into lines would be even better
    But, as you've discovered, the two are not equivalent.

    Try splitting the file not by any sort of Line break, but by a separator that is significant to HTML - the left angle-brace ("<"). That will give you an array item for each Html Element, some of which will start with "a " (or "A ", remember; case doesn't matter to HTML either) and those are the link elements that you're interested in.

    Regards, Phill W.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    190

    Re: Cannot read a file line-by-line...

    It doesn't matter anymore. Attempting to use Google's search repetitively causes the requests to eventually be ignored. Good luck using them to create your own aggregator. Thread closed.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width