Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Blade on August 02, 2004, 11:27 PM

Title: Writefile API problem
Post by: Blade on August 02, 2004, 11:27 PM
The Problem is that it prints wierd characters to the file instead of the string I tell it to. Here is my code.


My Declairs, types and constants.

Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal _
nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
lpOverlapped As OVERLAPPED) As Long

Public Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long

Public Declare Function GetFileSize Lib "kernel32" _
(ByVal hFile As Long, lpFileSizeHigh As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Public Const OFS_MAXPATHNAME = 128
Public Const OF_WRITE = &H1
Public Const GENERIC_WRITE = &H40000000


Public Type OVERLAPPED
       Internal As Long
       InternalHigh As Long
       offset As Long
       OffsetHigh As Long
       hEvent As Long
End Type

Public Type OFSTRUCT
       cBytes As Byte
       fFixedDisk As Byte
       nErrCode As Integer
       Reserved1 As Integer
       Reserved2 As Integer
       szPathName(OFS_MAXPATHNAME) As Byte
End Type



The sub.

Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, strData, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub




Usage.

dim arrData(2) as string
dim strData as string
dim strFile as string

     arrData(0) = "blah"
     arrData(1) = "ha"
     arrData(2) = "something"
     strData = join(arrData(), vbCrLf)
     strFile = app.path & "\textfile.txt"
     call savefile(strFile, strData)


The file is already created and is empty, after I call the savefile sub in my program it works with no errors but it prints wierd characters to the file. I've used GENERIC_WRITE also but that doesn't write anything to the file.  Whats wrong?
Title: Re:Writefile API problem
Post by: phvckmeh on August 03, 2004, 01:50 AM

Open (App.Path & "\log.txt") For Append As #1
Print #1,  "My String!"
Close #1


it will make log.txt if it does not exist
Title: Re:Writefile API problem
Post by: Blade on August 03, 2004, 01:09 PM
Yes I know that but I want to use the API because it's supposed to be faster. Any Idea why it doesn't work right?
Title: Re:Writefile API problem
Post by: Banana fanna fo fanna on August 03, 2004, 10:58 PM
It won't be noticeably faster.
Title: Re:Writefile API problem
Post by: Tuberload on August 03, 2004, 11:08 PM
Quote from: $t0rm on August 03, 2004, 10:58 PM
It won't be noticeably faster.
Compared to using a binary stream in VB, yes it is noticeably faster.
Title: Re:Writefile API problem
Post by: Blade on August 04, 2004, 04:19 PM
So any ideas whats wrong here?
Title: Re:Writefile API problem
Post by: Banana fanna fo fanna on August 04, 2004, 05:21 PM
Quote from: Tuberload on August 03, 2004, 11:08 PM
Quote from: $t0rm on August 03, 2004, 10:58 PM
It won't be noticeably faster.
Compared to using a binary stream in VB, yes it is noticeably faster.

If < .25sec than I don't consider it noticeable.
Title: Re:Writefile API problem
Post by: MyndFyre on August 04, 2004, 07:11 PM
Chances are that it would be noticeably faster if you were using many accesses in a row, but just for one file?  Pah.
Title: Re:Writefile API problem
Post by: Banana fanna fo fanna on August 04, 2004, 11:42 PM
A logfile, at that.
Title: Re:Writefile API problem
Post by: Tuberload on August 05, 2004, 01:39 AM
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?
Title: Re:Writefile API problem
Post by: Banana fanna fo fanna on August 05, 2004, 10:08 AM
Quote from: Tuberload on August 05, 2004, 01:39 AM
Now my question: wtf is "pah"?

Yes, I was wondering that myself.
Title: Re:Writefile API problem
Post by: Flame on August 05, 2004, 02:36 PM

Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub


Where does DBdata come from?  It looks to me like it should be strData
Title: Re:Writefile API problem
Post by: MyndFyre on August 05, 2004, 03:29 PM
Quote from: Tuberload on August 05, 2004, 01:39 AM
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?

Honestly, though, my statement was more fact than chance too.  I was just trying to preface it with something that would be more disarming.  Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.
Title: Re:Writefile API problem
Post by: Tuberload on August 05, 2004, 03:46 PM
Quote from: Myndfyre on August 05, 2004, 03:29 PM
Quote from: Tuberload on August 05, 2004, 01:39 AM
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?

Honestly, though, my statement was more fact than chance too.  I was just trying to preface it with something that would be more disarming.  Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.

You did not offend me, and that response was not directed solely towards you, that is why I did not quote you. Damned if I cannot find a way to make you believe that. I just stated a simple fact and everybody was arguing it. Thanks for the definition of pah btw.
Title: Re:Writefile API problem
Post by: Blade on August 05, 2004, 04:32 PM
Quote from: Flame on August 05, 2004, 02:36 PM

Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub


Where does DBdata come from?  It looks to me like it should be strData

Yes that was an error in me copying it to this forum and I fixed it here, but there is no such error in my code so whats wrong!
Title: Re:Writefile API problem
Post by: Banana fanna fo fanna on August 06, 2004, 11:03 AM
The fact of the matter is, you would've saved more developing time from learning how to use WriteFile and actually typing it, and also saved more maintenence time, than its worth in the very slight speed increase.
Title: Re:Writefile API problem
Post by: o.OV on August 12, 2004, 09:04 AM
1. The variables fileDB, lngRead, and DBdata pointless in your sample code

2. You can't pass a string variable directly to an API. You are suppose to use ByVal.

3. In your sample code you aren't even using the OVERLAPPED structure. You should just pass a NULL. Use of OVERLAPPED structure for Writing to a file is like APPEND in VB. Use of a NULL is like OUTPUT.