-
Feb 24th, 2012, 03:38 AM
#1
Thread Starter
Don't Panic!
Problems reading a file API
Hi,
I always seem to get errors with the APIs... sigh.
Right, yesterday code like this worked fine. Then I got a corrupted mdb with the code in and had to re-write it to the below.
The code below doesn't read the file. It's erroring with last dll error 5 (access denied) which is very weird as I have a segment of test code that I copied this from and that works.
So I must have missed something that is holding that file open.
Any ideas?
Code is to read byte by byte, find the vblf that doesn't have a vbcr before it and insert the vbcr, then output to a new file with the original name. Due to my vba code later to read the file and it failing due to no vbcrlf - original text file from a linux system I think...
Code:
strFNSrc = strFN & "_Orig.txt"
Name strFN As strFNSrc
lngFH_Dest = apiFileCreate(strFN, FileDA_Generic_Read, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
lngFH_Src = apiFileCreate(strFNSrc, FileDA_Generic_Write, FileSM_Read Or FileSM_Write, ByVal 0&, FileCD_OpenAlways, FileAtt_Normal, 0)
If lngFH_Src <> -1 And lngFH_Dest <> -1 Then
lngBytesToRead = 4096
lngBytesRed = lngBytesToRead
'dblFileSize = GetTheFileSizeAPI(lngFH_Src)
Do Until lngBytesRed < lngBytesToRead
ReDim aryBuffer(lngBytesToRead)
ReDim aryNewBuffer(lngBytesToRead + 512)
lngRes = apiFileRead(lngFH_Src, aryBuffer(0), lngBytesToRead, lngBytesRed, 0)
If lngRes = 0 Then Debug.Print Err.LastDllError
lngBytesToWrite = 0
If lngBytesRed > 0 Then
For lngL = 0 To lngBytesRed - 1
If aryBuffer(lngL) = 10 And Not blnPrevCR Then
aryNewBuffer(lngBytesToWrite) = 13
lngBytesToWrite = lngBytesToWrite + 1
aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
Else
aryNewBuffer(lngBytesToWrite) = aryBuffer(lngL)
End If
lngBytesToWrite = lngBytesToWrite + 1
Next
Else
txtProgress = Format(Now, "hh:nn:ss") & " - Failed to copy data file" & vbCrLf & txtProgress
End If
lngRes = apiFileWrite(lngFH_Dest, aryNewBuffer(0), lngBytesToWrite, lngBytesWritten, 0)
'---- store how much has been processed
dblFileDone = dblFileDone + lngBytesRed
PercBar boxPerc, txtProgress, dblFileDone, dblFileSize
Loop
lngRes = apiCloseHandle(lngFH_Src)
lngRes = apiCloseHandle(lngFH_Dest)
End If
End If
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|