VB6 File Input Errors

Hello, this is my first post on these forums. I hope there isn’t some stigma against signing up just to ask a question, but these forums looked like a fine place to recieve quality advice.

I’ve been making an mp3 player, and in which is a function to open .m3u playlist files. It had been working perfectly until I decided to expand some functions, and now it is broken. I have no idea what is wrong with the code, though.

If (strFileOpen <> "") Then
    Open strFileOpen For Input As #1
    Do
nextline:
        Input #1, strFile ' get next line

        If (Left(strFile, 1) = "#") Then GoTo nextline ' denotes file info, not path (typical winamp)
        If (Mid(strFile, 2, 1) <> ":") Then strFile = strFileOpenPath & strFile ' correct path

        If (FileLen(strFile) > 0) Then ' if file exists
            strFileArray = Split(strFile, "\")
            strFilePath = Replace(strFile, strFileArray(UBound(strFileArray)), "")
            strFile = strFileArray(UBound(strFileArray))
            Call AddItem(strFilePath, strFile) ' add file
            'Stop
        End If
        strFile = ""
    Loop While Not EOF(1)
End If

That’s a bit incomplete, but basically all it does is find the path/filenames within the line and calls a sub to put them on the playlist. The problem, though, is that I get a “Bad Name of File Number” error on the Input statement. If I put in On Error Resume Next it repeatedly feeds the first line of the file without going further… but in my experience every time Input (or Line Input) is called it reads the next line. There is never anything wrong with the path given for the Open statement, and it does read from the file, but no further than that first line.

Any help would be greatly appreciated. I don’t see any major problems so I’m guessing there’s something minor I continually overlook. Thanks in advance.

personally, for reading line by line, create a reference to scrrun.dll and use a textstream object rather than the method you use there. I find it more reliable.

Off the top of my head I would use the code as ;


Dim fso As FileSystemObject
Dim ts As textstream

If (strfileopen <> "") Then
    Set ts = fso.OpenTextFile(strfileopen, ForReading)
    'this loads the firstline
    strfile = ts.ReadLine
    'while addition to your loop
    Do While Not ts.AtEndOfStream
        
        Do While Left(strfile, 1) = "#"
            ts.ReadLine
        Loop
        'above loop seems to satisfy what you want to achieve, check the line is valid
        'If (Left(strfile, 1) = "#") Then GoTo nextline ' denotes file info, not path (typical winamp)
        If (Mid(strfile, 2, 1) <> ":") Then strfile = strFileOpenPath & strfile ' correct path

        'If (FileLen(strfile) > 0) Then ' if file exists
        If fso.FileExists(strfile) Then
            strFileArray = Split(strfile, "\")
            strFilePath = Replace(strfile, strFileArray(UBound(strFileArray)), "")
            strfile = strFileArray(UBound(strFileArray))
            Call AddItem(strFilePath, strfile) ' add file
            'Stop
        End If
        If Not ts.AtEndOfStream Then strfile = ts.ReadLine
    Loop
    
End If

That’s taking a pretty big guess on what some of the other bits do, your error should be removed by using the filesystemobject to read the file. As I said, a bit of guesswork, but good luck :smiley:

DT.

god DT how slack are you?

Spaztic, your welcome to use the forums, but do take your time to have a look about, you may find that we have more to offer than the answer to a quick question.

Some if not most are a little bit bonkers but we are generaly a friendly bunch

Is it safe to use filenumber 1 or should you use filenum = freefile?

Kevin, no other files are open during this procedure so there shouldn’t be a problem.

Sounds to me, Damski, that I’ll fit right in. I may make this forum a regular stop while I am browsing.

DoubleTop, thanks for your reply. Unfortunately I’ve never been formally taught or tried to teach myself how to properly call and use DLL’s. They seem very promising at that it could bring the applications I do to new levels, but they are yet something of a mystery to me. I’d gladly use your suggestion if I knew how, though.

From the advice of someone on IRC I instead opened the file in Binary mode and used Get to load the entire file into an array that can be parsed. That seems to work just fine and requires less work from the hard drive. Thanks for the help, though.