Announcement

Collapse
No announcement yet.

Data File has no Data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data File has no Data


    Hi all,

    What's up with my code?
    If I run this quick test, the Test.dat file length is zero.

    Please note that I purposely exited the loop so that I could
    see if the function was working... it is. Why is Test.dat
    zero length? Boggling my mind!

    here's the code:

    Code:
    #COMPILE DLL "c:\windows\system\pbSearch.dll"
    
    GLOBAL sContents AS STRING
    
    ' read the files
    FUNCTION sOFile(sFilename$) AS LONG
    
        LOCAL hFile AS LONG
    
        OPEN sFilename$ FOR BINARY AS #1
          sContents$ = SPACE$(LOF(1))
          GET #1, , sContents$
        CLOSE #1
    
        IF LEN(sContents$) THEN sOFile = 1 ELSE sOFile = 0
    
    END FUNCTION
    
    ' Copy files to Test.dat
    FUNCTION sCFile(sPathname$) EXPORT AS LONG
    
        LOCAL hFile AS LONG
    
        LOCAL sBuf AS STRING
        LOCAL sFile AS STRING
        LOCAL sPath AS STRING
        LOCAL sType AS STRING
    
        sPath$ = sPathname$
    
        IF RIGHT$(sPath$,1) <> "\" THEN sPath$ = sPath$ & "\"
    
        sType$ = sPath$ & "*.txt"
    
        sFile$ = DIR$(sType$)
    
        hFile = FREEFILE
    
        OPEN "c:\test.dat" FOR BINARY AS #hFile
    
        DO
          sFilename$ = sPath$ & sFile$
          IF sOFile(sFilename$) THEN
            IF LEN(sBuf$) < 5120000 THEN
              sBuf$ = sBuf$ & sContents$
              sFile$ = DIR$
            ELSE
              PUT #hFile, , sBuf$
              sBuf$ = ""
              EXIT DO       ' exit early to check data
              'sFile = Dir$
            END IF
          ELSE
             sFile$ = DIR$
          END IF
        LOOP UNTIL sFile$ = ""
    
        IF LEN(sBuf$)> 0 THEN
          PUT #1, , sBuf
        END IF
    
        FLUSH #hFile
        CLOSE #hFile
    
        sCFile = 1
    
    END FUNCTION

    Thanks in Advance!

    Mike


    mwm

  • #2

    Sorry guys... (you always see it after you post it)

    I finally got it right.....

    Here's my solution:

    Code:
    #COMPILE DLL "c:\windows\system\pbSearch.dll"
    
    GLOBAL sContents AS STRING
    
    ' read the files
    FUNCTION sOFile(sFilename$) AS LONG
    
        LOCAL hFile AS LONG
    
        OPEN sFilename$ FOR BINARY AS #hFile
          sContents$ = SPACE$(LOF(hFile))
          GET #hFile, , sContents$
        CLOSE #hFile
    
        IF LEN(sContents$) THEN sOFile = 1 ELSE sOFile = 0
    
    END FUNCTION
    
    ' Copy files to Test.dat
    FUNCTION sCFile(sPathname$) EXPORT AS LONG
    
        LOCAL hFile AS LONG
    
        LOCAL sBuf AS STRING
        LOCAL sFile AS STRING
        LOCAL sPath AS STRING
        LOCAL sType AS STRING
    
        sPath$ = sPathname$
    
        IF RIGHT$(sPath$,1) <> "\" THEN sPath$ = sPath$ & "\"
    
        sType$ = sPath$ & "*.txt"
    
        sFile$ = DIR$(sType$)
    
        hFile = FREEFILE
    
        OPEN "c:\test.dat" FOR BINARY AS #hFile
    
        DO
          sFilename$ = sPath$ & sFile$
          IF sOFile(sFilename$) THEN
            IF LEN(sBuf$) < 5120000 THEN
              sBuf$ = sBuf$ & sContents$
              sFile$ = DIR$
            ELSE
              PUT #hFile, , sBuf$
              sBuf$ = ""
              sFile$ = Dir$
            END IF
          ELSE
             sFile$ = DIR$
          END IF
        LOOP UNTIL sFile$ = ""
    
        IF LEN(sBuf$)> 0 THEN
          PUT #hFile, , sBuf
        END IF
    
        FLUSH #hFile
        CLOSE #hFile
    
        sCFile = 1
    
    END FUNCTION
    Anyone got any better ideas, please advise!

    Thanks
    mwm
    mwm

    Comment


    • #3
      Mike,

      I'm not sure how you are using your Fn's but it seems to me that
      you might be well served adding:

      Code:
      hFile = FREEFILE
      inside your sOFile function. It appears you are currently trying
      to use a Filenum of 0 (uninitialized LOCAL hFile).

      Your first example had a hard coded Filenum of 1 which probably
      matched the hFile value from the FREEFILE assignment in your sCFile
      function.

      You might also consider adding some error checking around file
      operations to make sure that they succeeded.

      Code:
      ErrorOccurred& = ERRCLEAR
      IF ErrorOccurred& THEN MSGBOX "Error"+STR$(ErrorOccurred&)+" during file I/O."


      ------------------
      Bernard Ertl
      Bernard Ertl
      InterPlan Systems

      Comment

      Working...
      X